About Question enthuware.ocpjp.v7.2.1514 :
Posted: Sun Jun 12, 2016 12:30 pm
Dears,
this is the first time so far that I don't agree with the answer: "will cause a thread that had called wait() on that object to be enabled for running"
For 2 reasons:
1) If you had 3 threads that all were waiting that object, then only 1 of the 3 will be enabled for running, the others will still be waiting and not runnable. Because notify() was used instead of notifyAll()
Hence you couldn't make a general statement saying "a thread that called wait() will become runnable (eligible for running)". Only for 1 out of 3 this statement works, so you cannot generalize it to all of them.
Right?
2) I was under the impression, that the thread receiving the notification, will not be immediately runnable (eg eligible for the JVM to pick it for running) because it first has to still get the lock on the object before it can continue running its code. Hence for me the thread might still be blocked before it can become runnable (eligible for running) eg this example (myObj is the same object shared across the 2 threads):
thread 1:
synchronized(myObj) {
try {
myObj.wait();
} catch(InterruptedException e) { }
System.out.printline("OK got notified and got the lock again on myObj, now I can continue this synchronised code block");
//more code
thread 2:
synchronized(myObj) {
myObj.notify();
try {
Thread.sleep(10000); //sleep atleast 10 seconds. Remember, sleep() holds the locks
} catch(InterruptedException e) { }
} //end of synchronized block on myObj, releasing the lock on myObj
In this situation, is it not so that for atleast 10 seconds, thread 1, even though it got notified, will not be runnable but instead is blocked?
I'm still learning so please don't kill me if i'm completely off base!!! But i want to be sure because intending to take the exam in the near future
this is the first time so far that I don't agree with the answer: "will cause a thread that had called wait() on that object to be enabled for running"
For 2 reasons:
1) If you had 3 threads that all were waiting that object, then only 1 of the 3 will be enabled for running, the others will still be waiting and not runnable. Because notify() was used instead of notifyAll()
Hence you couldn't make a general statement saying "a thread that called wait() will become runnable (eligible for running)". Only for 1 out of 3 this statement works, so you cannot generalize it to all of them.
Right?
2) I was under the impression, that the thread receiving the notification, will not be immediately runnable (eg eligible for the JVM to pick it for running) because it first has to still get the lock on the object before it can continue running its code. Hence for me the thread might still be blocked before it can become runnable (eligible for running) eg this example (myObj is the same object shared across the 2 threads):
thread 1:
synchronized(myObj) {
try {
myObj.wait();
} catch(InterruptedException e) { }
System.out.printline("OK got notified and got the lock again on myObj, now I can continue this synchronised code block");
//more code
thread 2:
synchronized(myObj) {
myObj.notify();
try {
Thread.sleep(10000); //sleep atleast 10 seconds. Remember, sleep() holds the locks
} catch(InterruptedException e) { }
} //end of synchronized block on myObj, releasing the lock on myObj
In this situation, is it not so that for atleast 10 seconds, thread 1, even though it got notified, will not be runnable but instead is blocked?
I'm still learning so please don't kill me if i'm completely off base!!! But i want to be sure because intending to take the exam in the near future