aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference between join() and wait() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference between join() and wait() method" Watch "difference between join() and wait() method" New topic
Author

difference between join() and wait() method

Thirugnanam Saravanan
Ranch Hand

Joined: Dec 13, 2007
Posts: 81
Hi all,

While studying about join() method and wait(),notify() method of Thread, I got little bit confused.

Lets assume there are two Threads Namely "One" and "Two" ,What is the difference between



and




Here both the code snippets are making sure that Thread "two" finishes first before Thread "One".

Can we use these two scenarios interchangeably or is there any specific reason to choose either of these approaches?


Saravanan
SCJP 5.0(98%), SCWCD 5.0 (100%), OCA
shilpa deshpande
Greenhorn

Joined: Sep 06, 2007
Posts: 7
Its a very good question, indeed. I would just attempt to answer it, please feel free to correct me. I think the inherent difference between join and wait is that
1) The thread that performs two.join() is going to wait until two thread finishes its job.
2) The thread which performs two.wait() waits only until two thread does some kind of notification and lock on the object can be obtained. two thread need not finish all its job but still can call notify() or notifyall().

I think that is why there uses are different. join is used in case of shutdown hooks and wait() notify() is used to notify the availability of resources.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
A look at the JavaDoc of Thread might be pretty helpful here.

Basically, wait() and notify() belong to class Object because myObject.wait() causes the calling thread to wait until some other thread calls myObject.notify() or myObject.notifyAll(). join(), in turn, belongs to class Thread because myThread.join() makes the calling thread wait until myThread dies.
Thirugnanam Saravanan
Ranch Hand

Joined: Dec 13, 2007
Posts: 81
Thanks shilpa and Guido for the Clarication.
Now I am clear.
neil johnson
Greenhorn

Joined: Dec 03, 2008
Posts: 24
Guido Sautter wrote:A look at the JavaDoc of Thread might be pretty helpful here.

Basically, wait() and notify() belong to class Object because myObject.wait() causes the calling thread to wait until some other thread calls myObject.notify() or myObject.notifyAll(). join(), in turn, belongs to class Thread because myThread.join() makes the calling thread wait until myThread dies.




What ever you said regarding wait and join is correct(wait belongs to Object, join belongs to Thread). What makes me confusing is that how you differ Object and Thread (I know Object is Super class).

Thread two = new Thread(runnable ref);

Is "two" considered as Object or Thread?

In the above example "two" is used to call both wait and join
myObject.wait() ---------> two.wait
myThread.join() ---------> two.join

Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183


When you just say

Thread two = new Thread(runnable ref);

its a Thread Object. Only after you start it (two.start()) , it becomes a Thread of execution.
neil johnson
Greenhorn

Joined: Dec 03, 2008
Posts: 24
Simran Dass wrote:

When you just say

Thread two = new Thread(runnable ref);

its a Thread Object. Only after you start it (two.start()) , it becomes a Thread of execution.




you are right. but my question is how you differentiate object and thread in terms of those two methods (join and wait). wait is called on object and join on thread.
we are calling both the methods after starting a thread. even then we are saying that wait is called by an object. why?
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
because anyways two will always be an object of class Thread.
after starting it it becomes a thread of execution but what after its run method completes??? Do you think the Thread object doesn't exist anymore???
If yes you are wrong, It will still be a Thread object its just it can't be a thread of execution anymore.

hope that clears your doubt.


SCJP 1.6 96%
neil johnson
Greenhorn

Joined: Dec 03, 2008
Posts: 24
Neha Daga wrote:because anyways two will always be an object of class Thread.
after starting it it becomes a thread of execution but what after its run method completes??? Do you think the Thread object doesn't exist anymore???
If yes you are wrong, It will still be a Thread object its just it can't be a thread of execution anymore.

hope that clears your doubt.



I don't know whether i am confusing the people with the way i am asking. My question is why we are using two terms thread and object instead of using just a thread while calling join and wait. because in both the cases we are calling using an alive thread.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

It's simply because when you call the join() method on a Thread object, the behaviour of the method is related to the behaviour of the thread (you've read the API documentation so you know what I mean).

Whereas when you call the wait() method on a Thread object, the behaviour is identical to the behaviour when you call it on a String object or a File object or any other Object.

That's the difference.
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
Neil , i can try to answer your question but i m not a big expert in Java; my explanation may or may not be correct:

wait() is a method of class Object and it is a thread-related method ( you can go to API and read the explanation of wait method; also in K & B page 718)

So it is clear that wait is invoked on an object which is a Thread ( Thread Object).

Now your question comes , why is it not just a Thread, why are we calling it an object while in join() we say it is invoked on a thread.. no reference to an object.

Remember , for using wait(), we have a condition.
And the answer lies in that condition : the wait() method is invoked from a synchronized context and the thread calling wait must own the lock of the object on which the wait() is called. Remember, when a thread enters a synchronized method or code block of a class , the thread acquires the lock on the whole object , and no other thread can access any synchronized context of that object.

This is not the condition in case of join() . There is no such condition of a synchronized context and the possession of object's lock. It just joins one thread at the end of another.


So in case of wait() , we are using two terms Thread and Object because wait() method has to be called from a synchronized context and the thread calling wait() must own the lock on the object (Thread Object) on which wait() is called .The thread releases the lock. The thread calling wait() waits (is blocked) until it gets the lock and the notify() method is called.

(Hope this clarifies your confusion.If anyone finds a mistake in my explanation ,please correct me.)


SCJP 6.0 (86%) SCWCD 5 (94%)
thanvi janu
Greenhorn

Joined: Nov 16, 2009
Posts: 29
Hi saima,

So in case of wait() , we are using two terms Thread and Object because wait() method has to be called from a synchronized context and the thread calling wait() must own the lock on the object (Thread Object) on which wait() is called .The thread releases the lock. The thread calling wait() waits (is blocked) until it gets the lock and the notify() method is called.


can you explain with simple code please.

Thank a lot,
Thanvi

saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
Hi Thanvi,

consider the following code:



the output is : ThreadMain
ThreadA (100 times)
Back in ThreadMain

Hope you understand by this simple code.



thanvi janu
Greenhorn

Joined: Nov 16, 2009
Posts: 29
Thanks saima.
neil johnson
Greenhorn

Joined: Dec 03, 2008
Posts: 24
saima kanwal wrote:Hi Thanvi,

consider the following code:



the output is : ThreadMain
ThreadA (100 times)
Back in ThreadMain

Hope you understand by this simple code.






I doubt about the code. Because as far as i know you can't start a thread more than once. Correct me if I am wrong. Any how thanks for such a nice explanation you gave for Object vs Thread.
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
yes neil you are right. I have rewritten t.start() in the synchronized (t) brackets, which is an error (a typographical error). The correct code goes like this :



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between join() and wait() method