user2080568
user2080568

Reputation: 93

Regarding joining of threads in an order

I am a new bie to the world of threads , still learning, as I was going through the concept of threads and join where other thread wait for earlier thread to be completed and join it from that end, could you please advise me that that I want to start three threads T1,T2,T3 in which t2 will start once T1 is completed .

Upvotes: 0

Views: 75

Answers (3)

ddmps
ddmps

Reputation: 4380

Of what I understand you want to wait until Thread 1 is completely done and then start Thread 2, while thread 3 can run anywhere. Simple code that I think fulfills your question:

Thread thread1 = new Thread1();
Thread thread2 = new Thread2();
Thread thread3 = new Thread3();
thread3.start();
thread1.start();
try {
  thread1.join();
  thread2.start();
} catch (InterruptedException e) {
  //if you do not use thread1.interrupt() this will not happen.
}

Upvotes: 2

exexzian
exexzian

Reputation: 7890

Do something like this:

        Thread T1 = new Thread(new ThreadExm);  // where ThreadExm implements Runnable
        Thread T2 = new Thread(new ThreadExm);

        try {
            // Start the thread1 and waits for this thread to die before
            // starting the thread2 thread.
            T1.start();
            T2.join();

            // Start thread2 when T1 gets completed
            thread2.start();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

Upvotes: 0

patrickuhlmlann
patrickuhlmlann

Reputation: 342

You can use Barriers to start some action (maybe another thread) after a number of threads finished.

Check: http://programmingexamples.wikidot.com/java-barrier for more information.

But to wait for only one thread really doesn't make to much sense...

Upvotes: 0

Related Questions