*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enthuware Q52342 on concurrency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enthuware Q52342 on concurrency" Watch "Enthuware Q52342 on concurrency" New topic
Author

Enthuware Q52342 on concurrency

Paolo Dina
Ranch Hand

Joined: Aug 15, 2008
Posts: 63
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.


SCJP 5
himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
T2 will reamin blocked till obj.notify(); is not called!!


SCJP 5
Paolo Dina
Ranch Hand

Joined: Aug 15, 2008
Posts: 63
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
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...


------------------------
Bob
SCJP - 86% - June 11, 2009
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3254
    
    2
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?

Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Paolo Dina
Ranch Hand

Joined: Aug 15, 2008
Posts: 63
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enthuware Q52342 on concurrency
 
Similar Threads
How to avoid Deadlock (Wait with two locks)
Thread and variable value question?
Thread Question
A question about using Join in Thread.
Thread-Synchronized ?