XSLT存档  

不及格的程序员-八神

 查看分类:  ASP.NET XML/XSLT JavaScripT   我的MSN空间Blog

When to use join method in Java

 

As I said join is an important and useful method from Thread class but many times overlooked. Similar to wait for method, by using join method, we can make one Thread to wait for another. The primary use of Thread.join() is to wait for another thread and start execution once that Thread has completed execution or died. Join is also a blocking method, which blocks until the thread on which join has called die or specified waiting time is over. 

 

By the way, understanding, how to join method works, is not straight forward. Many developers get confused on things like, which thread will wait,  which thread will join etc. These points will be clearer when we go through an example of joining multiple Thread in Java using join() method.

 

336x280_Middle_Of_Post

 

 

Thread Join Example  in Java

Here is a simple example of joining two threads using Thread.join() method. By the way unlike Thread.sleep() method, join() is not a static method, it needs to be call on a java.lang.Thread object. The current thread, which calls join method will wait until the thread on which join has called die or wait at most specified millisecond for this thread to die.

 

/**

 * Sample Java class to illustrate How to join two threads in Java.

 * join() method allows you to serialize processing of two threads.

 */

 

public class Join {

  

    public static void main(String args[]) throws InterruptedException{

      

        System.out.println(Thread.currentThread().getName() + " is Started");

      

        Thread exampleThread = new Thread(){

            public void run(){

                try {

                    System.out.println(Thread.currentThread().getName() + " is Started");

                    Thread.sleep(2000);

                    System.out.println(Thread.currentThread().getName() + " is Completed");

                } catch (InterruptedException ex) {

                    Logger.getLogger(Join.class.getName()).log(Level.SEVERE, null, ex);

                }

            }

        };

      

        exampleThread.start();

        exampleThread.join();

      

        System.out.println(Thread.currentThread().getName() + " is Completed");

    }

  

}

  

 

Output:

main is Started

Thread-0 is Started

Thread-0 is Completed

main is Completed

 

If you look at above example, the first main thread is started and then it creates another thread, whose name is "Thread-0" and started it. Since Thread-0 sleep for 2 seconds, it requires at least 2 seconds to complete and in between main thread called join method on the Thread-0 object. Because of join method, now, main thread will wait until Thread-0 completes its operation or You can say main thread will join Thread-0. If you look at output, it confirms this theory.

 

Important point on Thread.join method

Now we know How to use join method in Java, it’s time to see some important points about Thread.join() method.

 

1. Join is a final method in java.lang.Thread class and you cannot override it.

2) Join method throws IntrupptedException if another thread interrupted waiting for thread as a result of join() call.

 

3) Join is also an overloaded method in Java, three version of join() available, check Javadoc for details.

 

That’s all on How to join two Threads in Java with an example. You can Join multiple threads by using Thread.join() method. Join is particularly useful to make one thread wait for other, or serializing two function e.g. first load your cache and then start processing the request.

 

Related Java multithreading Tutorials from Javarevisited Blog

How to use countdown Semaphore in Java with example

Why to wait and notify method to get called from synchronized block in Java

How synchronization works in Java

How volatile keyword works in Java

Java mistake 2 – Mixing static and non-static synchronized methods

 

 

Read more: http://javarevisited.blogspot.com/2013/02/how-to-join-multiple-threads-in-java-example-tutorial.html#ixzz4g4yeCn8X

posted on 2017-05-04 12:02  不及格的程序员-八神  阅读(12)  评论(0编辑  收藏  举报