Java Thread : Difference between wait(), join() and sleep()

wait()

The call of wait() method on the object will hold the execution of the currently running thread until some other thread calls the notify() or notifyAll() method on the same object. The currently running thread will release the object monitor of the object on which the wait() method is invoked/called. Then some other thread can acquire the object monitor and continue its work with that object. The thread (that owns the object monitor) will call the notify() or notifyAll() method to notify the threads that are waiting on the on that object to inform that the object monitor will be released soon and get ready to acquire it.

wait() method should be called in a synchronized context. that means either in synchronized method or synchronized block.

since the wait() method is inherited from the Object class, it can be invoked on any java object(that is directly or indirectly inherited from Object class)

Click here to see my blog article about wait() method with code examples.

 

join()

The join() method belongs to the Thread class. therefore join() method can be invoked only on the Thread objects. The join() method is used to postpone the execution of the currently running thread after the execution of some other thread.

ThreadA threadA =  new ThreadA();
threadA.join();

Here the currently running thread (lets say it as the main thread)  will hold the execution until the threadA completes. Once the threadA is completed, the waiting/sleeping thread (the main thread) will be resumed its execution.  Simply the main thread will be resumed once the threadA  is dead(after completing the run method)

Click here to see my blog article about join() method with code examples.

 

sleep()

Thread.sleep causes the current thread to suspend execution for a specified time period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.

Click here to see my blog article about sleep() method with code examples

 

The more comparison of wait(), sleep() and join method can be found on my next blog article.

 

3 thoughts on “Java Thread : Difference between wait(), join() and sleep()

  1. I’m no longer sure where you are getting your information, but great topic. I must spend a while finding out more or figuring out more. Thanks for great info I was on the lookout for this info for my mission.

    Liked by 1 person

  2. hi!,I love your writing very a lot! percentage we keep up a correspondence more approximately your article on AOL? I need an expert in this area to solve my problem. Maybe that’s you! Having a look ahead to see you.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s