Thursday, February 20, 2020

Difference between wait() and join method in Java thread

Though both wait() and join() methods are used to pause the current thread and have a lot of similarities they have different purposes. One of the most obvious differences between the wait() and join() method is that former is declared in java.lang.Object class while join() is declared in java.lang.Thread class. This means that wait() is related to the monitor lock which is held by each instance of an object and join method is related to Thread itself. The wait() method is used in conjunction with notify() and notifyAll() method for inter-thread communication, but join() is used in Java multi-threading to wait until one thread finishes its execution.

 Another difference between wait() and join() method is that former must be called from synchronized method or block but later can be called without a synchronized block in Java.

A good knowledge of differnet thread related methods e.g. start and run, wait and notify, join and yield goes a long way in writing robuts and correct multi-threaded, concurrent Java applications. If you don't know how to use join method, you miss out on writing program where one thread needs to wait for the completion of other thread before it starts.

These were some fundamental difference between wait() and join() method, let's see a couple of more differences and similarities in detail.

Similarities between wait() and join()


Before looking at the difference between wait() and join() method of thread class, let's see some of the key similarities between them:

1) Both wait() and join() is used to pause the current thread in Java. In the first case, the thread which calls the wait() method goes into waiting for the state while in the second case the thread which calls the join() method goes into waiting for the state until the thread on which join has been called finishes its execution.

Oracle Java Tutorial and Material, Oracle Java Certification, Oracle Java Prep, Oracle Java Exam Prep

2) Both wait() and join() are overloaded in Java. You can find a version of both the wait() and join() which accepts a timeout parameter.

3) Both wait() and join() can be interrupted by calling interrupt() method in Java. Which means you can handle the interrupt by catching InterrruptedException in Java.

4) Both wait() and join() are a non-static method and cannot be called from the static context.

Difference between wait() and join() method of thread


So far you have learned what is wait() and join() method, why they are used and some similarities between them. Now is the time to revise and find out some key differences between them.

1) First and foremost difference between wait() and join() method is that wait() is declared and defined in Object class while join() is defined in the thread class.

2) Second important difference between wait() and join() is that wait() must be called from synchronized context i.e. synchronized method or block otherwise it will throw IllegalMonitorStateException but there is no such requirement for calling the join() method in Java.

On the other hand, you can call join() method with and without synchronized context in Java.

Oracle Java Tutorial and Material, Oracle Java Certification, Oracle Java Prep, Oracle Java Exam Prep

3) The third difference between wait() and join() method is that the thread calls wait for () method it releases any lock held for the object on which wait() is called, but calling join() method doesn't release any monitor or lock.

4) Another difference between them is that wait() is used for inter-thread communication while the join() is used for adding sequencing between multiple threads e.g. one thread starts execution after first thread finishes its execution.

5) You can awake a thread waiting by calling wait() method of the object class by using notify() and notifyAll() method but you can not break the waiting imposed by join without interruption or unless the thread on which join is called has finished execution.

That's all about the difference between wait() and join() method in Java. Use wait() when you want inter-thread communication e.g. to solve the producer-consumer problem while using the Thread.join()  method when you want one thread to start execution only if the first thread finishes its execution.

Related Posts

0 comments:

Post a Comment