aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads are there to wait and notify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads are there to wait and notify" Watch "Threads are there to wait and notify" New topic
Author

Threads are there to wait and notify

Vidyakar Sharma
Greenhorn

Joined: Dec 27, 2009
Posts: 17
Hi All,
Why wait(), notify() and notifyAll() methods are there in the Object class and why not in the Thread class while threads are the objects which keeps waiting or notifies another threads? Please help me.


Vidyakar Sharma.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3267
    
  52

From the documentation,
Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).


In particular, a wait() call makes the thread, which is executing that statement, to wait until a notify() or notifyAll() method is invoked on the same object where the wait() method is called.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Vidyakar Sharma
Greenhorn

Joined: Dec 27, 2009
Posts: 17
Probably question was not interpreted properly.
Vidyakar Sharma
Greenhorn

Joined: Dec 27, 2009
Posts: 17
Vidyakar Sharma wrote:Hi All,
Why wait(), notify() and notifyAll() methods are there in the Object class and why not in the Thread class while threads are the objects which keeps waiting or notifies another threads? Please help me.

Greenhorns stay away this is not for you atleast.
Vidyakar Sharma
Greenhorn

Joined: Dec 27, 2009
Posts: 17
Somebody please answer.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3267
    
  52

Vidyakar Sharma wrote:Somebody please answer.

You waited less than 5 minutes for a response? Please understand that PatienceIsAVirtue

a wait() call makes the thread, which is executing that statement, to wait...
If it was only in the Thread class, you will be able to make any other thread (even that thread doesn't own the monitor lock of the object) to wait, which is explicitly prohibited by the JLS. Same thing goes for the other two methods as well.
From the documentation...
This method should only be called by a thread that is the owner of this object's monitor.

On another hand, we simply call obj.notifyAll() to to notify the all threads which are waiting for this obj object. How do you implement such a thing by declaring this method only in the Thread class?
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Hi Devaka Cooray,

Can you explain it further.
I am not able to understand the concept you are talking about
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi Vidyakar,

Please wait for sometime when you post some question, when some volunteer will see the question and will have ample
time, they will automatically answer your question. Like Devaka quoted, patience is a virtue.

He means that if methods like wait() were declared in the Thread class itself, then one thread will be able to make the other thread wait
even if the other thread is not having the object lock and such a practice should be prohibited as recommended by JLS.

Additionally, notifyAll() is basically used to notify the threads waiting for a particular object. You can of thread as a particular process.
You acquire locks on objects not on threads, so how such a functionality to notify other threads can be implemented if such methods
were present in the thread class. Each thread will have its private state, its own call stack. In that case, every thread has to share its
call stack i suppose if they were implemented in the Thread class.

I am though not able to explain this scenario very well but this is what i picked up. Correct me Devaka if i am wrong.

Best Regards,

Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

Vidyakar Sharma wrote:Probably question was not interpreted properly.


If you believe that the question was not interpreted properly, then please elaborate. To just imply a wrong answer is a bit silly, especially since, to know a wrong answer the OP needs to know the right one.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

Vidyakar Sharma wrote:
Why wait(), notify() and notifyAll() methods are there in the Object class and why not in the Thread class while threads are the objects which keeps waiting or notifies another threads? Please help me.


Because locks (synchronization) and condition variables (wait/notify) are independent of the thread object. A thread can have many locks, and many condition variables around data that is being protected. To have it only around specific threads objects will imply that they can only wait on a general purpose notification object, which is not correct.

Having said that, the wait/notify model, is also not correct. Having the lock and the condition variable being the same object (the Object instance) forces the two enitities together. The main issue is that it is not possible to have two condition variables that share the same lock -- which is a somewhat common occurance. The correct way to do this -- which was included via Java 5 -- is to have a separate Lock and Condition class.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads are there to wait and notify