Я не знаю, как печатать в текстовом файле, когда я использую потоки, потому что каждый раз, когда он просто создает другой файл, я получаю только один результат, который является последним, я пробовал много вещей и всегда одно и то же.Как печатать в текстовом файле, если я использую потоки в java?
Это всего лишь часть кода, помимо печати в файл, мне тоже приходится печатать график, и у меня такая же проблема, как и для каждого потока.
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();
}