Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithread Write into Text File

Hope you can help me with this one. I need to make a program that, using multiple threads, writes into a text file. What i need is 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, writing at the same time.

Here's my code.

Method 1: Using a "for" to create and start 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 give it as a parameter to create a ThreadCustom object, so all the threads use the same PrinterWriter object (All objects in java are passed as reference, right?)

What im expecting to get? Something like this

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.

Hope you can help me!

Thanks in advance community!!

PS: Using the .start() does create the .txt, but doesnt write anything on it, but, if i use .run() instead of .start(), it does write into the .txt, but sequentially (ID0, ID1, ID3, and so on)

like image 723
Diego Avatar asked Oct 09 '22 21:10

Diego


1 Answers

You have to wait for your threads to complete their work before closing the output. Simplest 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();
like image 140
Alexander Pogrebnyak Avatar answered Oct 12 '22 11:10

Alexander Pogrebnyak