• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware Q52342 on concurrency

 
Paolo Dina
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Literally says

Consider the following situation:
Thread T1 holds the lock for an object Obj. Thread T2 is has called obj.wait() and is blocked.
What will allow the thread T2 to become runnable?


The right answer accordingly to them is "Thread T1 releases the lock on Obj and calls the notify() method on Obj", but I knew that T1 first has to call notify() and then, consequently, the lock on the object will be released (when T1 is done with its runnable run() method).

Am I wrong? Thanks.
 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
T2 will reamin blocked till obj.notify(); is not called!!
 
Paolo Dina
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
himanshu kesarwani wrote:T2 will reamin blocked till obj.notify(); is not called!!


False sir. T2 will remain blocked till the lock on obj has not been released by the thread that owns it. And most important, this moment doesn't necessarily happen *just after* the call to notify(). Can someone confirm?

 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paolo you are right at your point. A thread has to call notify on an object before releasing the lock on that object not after releasing the lock. But I think you went too deep into the explanation . The statement didn't mention the order of release of lock and calling notify. Although the sentence mentions releasing the lock before calling notify, but it doesn't say anything about the order of these two steps...
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question's meaning can change based on the assumptions that we make on the data that has not been presented. The question by itself may not make sense under some assumptions

For example how can a thread call wait() without first obtaining that Object's lock ? If that lock is already obtained the thread will block forever, assuming the lock will never be released. The question however mentions that wait() was successfully called and blocked. Just my thoughts
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suspicion here is that the notify() or notifyAll() call just causes waiting threads to be changed to a runnable state. It most likely does not start any re-dispatching activity. The clamor for the lock doesn't start until after the thread that holds the lock exits the locked block/method. I believe it is at this point will be the soonest one could expect to see the thread manager thinking about re-evaluating it's dispatch of another thread. And, as all the books point out, it doesn't HAVE to do it right at that moment...
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Ruth wrote:My suspicion here is that the notify() or notifyAll() call just causes waiting threads to be changed to a runnable state. It most likely does not start any re-dispatching activity. The clamor for the lock doesn't start until after the thread that holds the lock exits the locked block/method. I believe it is at this point will be the soonest one could expect to see the thread manager thinking about re-evaluating it's dispatch of another thread. And, as all the books point out, it doesn't HAVE to do it right at that moment...

Technically notify() doesn't cause a waiting thread to become runnable. It causes it to go from the waiting state to the blocking state. When the original thread releases the lock on the object, then the originally waiting thread will go from blocking to runnable.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3764
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paolo Dina wrote:Literally says

Consider the following situation:
Thread T1 holds the lock for an object Obj. Thread T2 is has called obj.wait() and is blocked.
What will allow the thread T2 to become runnable?


The right answer accordingly to them is "Thread T1 releases the lock on Obj and calls the notify() method on Obj", but I knew that T1 first has to call notify() and then, consequently, the lock on the object will be released (when T1 is done with its runnable run() method).

Am I wrong? Thanks.


Paolo, Your confusion is very genuine. Even though the answer does not explicitly say anything about the order, it does give an impression that Thread T1 first releases the lock and then calls notify, which cannot actually be done (notify/notifyAll can only be called while the thread has the lock). It should be made clearer.

Two things must happen for T2 to become runnable - 1. T1 must call notify/notifyall and T1 must also release the lock i.e. exit the synchronized block (but not necessarlily the run() method).

Can you please give the complete question id so that we can locate the question?
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Bala wrote:For example how can a thread call wait() without first obtaining that Object's lock ? The question however mentions that wait() was successfully called and blocked. Just my thoughts


Agree. Question says T1 holds the lock, if so that gives a wrong impression how come T2 call wait() . Anyway when the authors get to the complete question we'll able get some clarifications on that.
 
Paolo Dina
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anil wrote:
Can you please give the complete question id so that we can locate the question?

Of course, QID is: com.enthuware.ets.scjp.v5.2.342

Btw, thanks for all your answers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic