• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads are there to wait and notify

 
Vidyakar Sharma
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4495
267
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vidyakar Sharma
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably question was not interpreted properly.
 
Vidyakar Sharma
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody please answer.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4495
267
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Devaka Cooray,

Can you explain it further.
I am not able to understand the concept you are talking about
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Henry Wong
author
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic