Multithreaded write to text file

I hope you help me with this. I need to make a program that, using multiple threads, writes to a text file. I need to show how the processor gives β€œattention” to one thread or another, so basically I need all the threads running at the same time and, of course, recording at the same time.

Here is my code.

Method 1. Use "for" to create and run threads.

public class ThreadGenerator { public static void main(String[] args) { File textFile = new File("c:\\threadLog.txt"); try { PrintWriter out = new PrintWriter(new FileWriter(textFile)); for (int index = 0; index < 5; index++) { ThreadCustom thread = new ThreadCustom("ID" + index, out); thread.start(); } out.close(); } catch (IOException ex) { Logger.getLogger(ThreadGenerator.class.getName()).log(Level.SEVERE, null, ex); } } } 

Method 2. Creating and starting each thread manually

 public class ThreadGenerator { public static void main(String[] args) { File textFile = new File("c:\\threadLog.txt"); try { PrintWriter out = new PrintWriter(new FileWriter(textFile)); ThreadCustom thread1 = new ThreadCustom("ID1", out); ThreadCustom thread2 = new ThreadCustom("ID2", out); ThreadCustom thread3 = new ThreadCustom("ID3", out); ThreadCustom thread4 = new ThreadCustom("ID4", out); ThreadCustom thread5 = new ThreadCustom("ID5", out); thread1.start(); thread2.start(); thread3.start(); thread4.start(); thread5.start(); out.close(); } catch (IOException ex) { Logger.getLogger(ThreadGenerator.class.getName()).log(Level.SEVERE, null, ex); } } } 

This is my ThreadCustom class.

 public class ThreadCustom extends Thread { private String threadId; private PrintWriter out; public ThreadCustom(String threadId, PrintWriter out){ this.threadId = threadId; this.out = out; } @Override public void run(){ DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); Calendar cal = Calendar.getInstance(); this.out.println("Thread ID: "+this.threadId+" Enter Time: "+cal.getTime()+"\n"); for(int index = 0; index < 10000; index++){ this.out.println("Thread ID: "+this.threadId+" Current Time: "+cal.getTime()+"\n"); } this.out.println("Thread ID: "+this.threadId+" Exit Time: "+cal.getTime()+"\n"); } } 

So, as you can see, I create a PrinterWriter and provide it as a parameter to create a ThreadCustom object, so all threads use the same PrinterWriter object (all objects in java are passed as a link, right?)

What do I expect to receive? Something like that

 Thread ID: ID0 Enter Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID1 Enter Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID1 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID1 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID Exit Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID1 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012 Thread ID: ID1 Exit Time: Fri Jan 27 00:38:54 CLST 2012 

or something like that.

I hope you help me!

Thank you in advance!

PS: Using .start () does create .txt, but writes nothing on it, but if I use .run () instead of .start (), it writes to .txt, but sequentially (ID0, ID1, ID3, etc.). d.)

+4
source share
1 answer

You need to wait for your threads to complete before closing the output. The easiest way to do this is to use join .

  thread1.start(); thread2.start(); thread3.start(); thread4.start(); thread5.start(); thread1.join(); thread2.join(); thread3.join(); thread4.join(); thread5.join(); out.close(); 
+3
source

Source: https://habr.com/ru/post/1393291/


All Articles