Searching...
Sunday, June 30, 2013

How to Join Multiple Threads in Java

2:29 AM
How to Join Multiple Threads in Java
How to Join Multiple Threads in Java

join method from Thread class is an important method and used to impose order on execution of multiple Threads. Concept of joining multiple threads is very popular on  mutithreading interview question. Here is one of such question, “You have three threads T1, T2 and T3, How do you ensure that they finish in order T1, T2, T3 ?. This question illustrate power of join method on multithreaded programming. Unlike classical thread questions like difference between wait and sleep method or solving producer consumer problem in Java, This one is bit tricky. You can do this by using join method, by calling T1.join() from T2 and T2.join() from T3. In this case thread T1 will finish first, followed by T2 and T3. In this Java multithreading tutorial we will have a closer look on join method with a simple example. Idea is to illustrate how join method works in simple words. By the way from Java 5 onwards you can also use CountDownLatch and CyclicBarrier classes to implement scenarios like one thread is waiting for other threads to finish there task.

How to Join Multiple Threads in Java
Next
This is the most recent post.
Previous
This is the last post.

0 comments:

Post a Comment