wait-and-join-method-of-thread-java

Similarities:
1.    both wait() and join() methods are used to pause the current thread.

2.    Both the threads calls wait() and join() methos respectively moves to the waiting state.

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

4.    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.


Differences :
1.    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 class.

2.    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.

3.    wait() must be called from synchronized method or block but join() can be called without a synchronized block in Java.

4. Thread which calls the wait() method relinquishs the object lock but thread which calls join() method does not relinquish the object lock.



SHARE

esant technology

0 comments :

Post a Comment