简体   繁体   English

如果我在Java中使用线程,如何打印到文本文件?

[英]How to print to a text file if I'm using threads in java?

I don't know how to print to a text file when I'm using threads because every time it just creates another file, so I end up with just one result which is the last one, I have tried a lot of things and is always the same. 在使用线程时,我不知道如何打印到文本文件,因为每次它仅创建另一个文件时,我最终只有一个结果,即最后一个,我已经尝试了很多事情,并且总是一样。

This is just a part of the code, besides printing to the file I have to print a graph too and I have the same problem as it creates one graph for each thread. 这只是代码的一部分,除了打印到文件之外,我还必须打印一个图,而且我也遇到同样的问题,因为它为每个线程创建一个图。

public class Adsda implements Runnable{
    private  int id=0;
    public int number;
    public String file="Time.txt";
    private final PrintWriter outputStream;

    public Adsda(int id) throws FileNotFoundException {
        this.id=id+1;
        this.outputStream=new PrintWriter(this.file);
    }

    public void run() {
        int i,fact=1;  
        this.number=id;//It is the number to calculate factorial    
        long A=System.nanoTime();
        for(i=1;i<=this.number;i++){    
            fact=fact*i;    
        }
        long B=System.nanoTime();
        long t=B-A;
        double tt = (double)t / 1000000000.0;
        System.out.println("Factorial of "+number+" is: "+fact+" Time: "+tt);
        this.outputStream.println("Factorial of: "+this.number+" Time: "+tt);
        this.outputStream.flush();
    }

    public static void main(String[] args) throws FileNotFoundException{  
        ExecutorService executor = Executors.newFixedThreadPool(2);//creating a pool of 2 threads  

        for(int i=0;i<5;i++){
            executor.submit(new Adsda(i) );
        }

        executor.shutdown();
    }

You should create a single PrintWriter and share that with the threads by passing it in the constructor instead of having each thread create their own PrintWriter (and file). 您应该创建一个PrintWriter并通过在构造函数中传递它与线程共享,而不是让每个线程创建自己的PrintWriter (和文件)。 Although that will result in the file containing the results in weird order. 尽管那样会导致文件中包含奇怪顺序的结果。 If you want to have them in a specific order, you should have the threads output their results in their own buffers and when all threads are finished, write the buffers out to a file in sequence. 如果要按特定顺序排列它们,则应让线程将它们的结果输出到它们自己的缓冲区中,并且当所有线程完成后,按顺序将缓冲区写出到文件中。

PrintWriter pw = new PrintWriter(filename);

for(int i=0;i<5;i++){
    executor.submit(new Adsda(i, pw) );
}

Just to answer your question, you have multiple threads that execute your run method and all of them will write the file named "Time.txt". 只是为了回答您的问题,您有多个执行您的run方法的线程,所有线程都将写入名为“ Time.txt”的文件。 You should write a file for each thread. 您应该为每个线程编写一个文件。 Also you are sharing your output stream between multiple threads which is an issue in itself. 另外,您正在多个线程之间共享输出流,这本身就是一个问题。 Move the print writer creation in the run method and use a name like "time" + Thread.curentThread().getID() + ".txt". 在run方法中移动打印编写器的创建,并使用“ time” + Thread.curentThread()。getID()+“。txt”之类的名称。 That should fix it. 那应该解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM