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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

lakshmi nair
Ranch Hand

Joined: Oct 11, 2000
Posts: 63
This is a question from cert. study guide by Barry Boone.
Which statements are true concerning the method notify() that is used in conjenction with wait() ?
a) If there is more than one thread waiting on a condition, only the thread that has been waiting the longest is notified.
b) If there are more than one thread waiting on a condition, there is no way to predict which thread will be notified.
c) notify() is defined in Thread class
d) Its not strictly necessary to own the lock for the object for which you invoke notify()
e) notify() should only be invoked from within a while loop.
I think the answer is (b) but it is given as (e) in the book.
Any idea?
Thanks
Lakshmi

[This message has been edited by lakshmi nair (edited October 24, 2000).]
[This message has been edited by lakshmi nair (edited October 24, 2000).]
Viji Bharat
Ranch Hand

Joined: Sep 18, 2000
Posts: 101
Lakshmi:
I also think (b) is correct. Can't comment on choice (e). Am waiting for an explanation.
Amit Tyagi
Ranch Hand

Joined: Oct 18, 2000
Posts: 52
Hi,
I think (b) should be not be right. Thread with heighest priority will be notified and ....thus making that thread to Runnable state.....
have to think about the last (e)..........
May be it is like this....
we should invoke notifyAll() method instead of invoking notify from the while loop.
not sure.... please somebody ... explain....

Amit
P SOLAIAPPAN
Ranch Hand

Joined: Oct 20, 2000
Posts: 68
Hi Lakmi,
You gone through the book wrongly. You read the ans: correctly given in page No 585 of Barry Bone
It is given (b) only
Actualy it is given like this
57.b.a,cand d are definetly false
Note: b. (This is the Ans Confusions due to no space between ans.
solaiappan
P SOLAIAPPAN
Ranch Hand

Joined: Oct 20, 2000
Posts: 68
Hi,
This is from JAVA API
"Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. "The choice is arbitrary and occurs at the discretion of the implementation". A thread waits on an object's monitor by calling one of the wait methods.
So ans is (b)
In Barry bone book it is given For (e) While loop is a common construct, But it is not necessary (again stressed "not necessary") to invoke notify()&wait() in a while loop.
Hence (e) is wrong
solaiappan

bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Here are my thoughts:
A. Is false because you can't determine which thread will start, thread scheduler does this, and it doesn't really even know at the time.
B. True, for the reason above.
C. False since notify() is defined in the Object Class not Thread
D. False since notify() has to be in a synchronized block and therefore must own the lock.
E. False since while it is common to have them in a while loop, it is not necessary. You could do it in a for loop, do loop, or even without the loop I beleive. But it is common to do this in a while loop. But since it said ONLY, it is false.
Those are my thoughts.
Viji Bharat
Ranch Hand

Joined: Sep 18, 2000
Posts: 101
All:
If there are more than one thread waiting on the same object and they have different priorities, which thread gets notified first? If priorities play a role in this process, then answer (b) might have to be revisited.
I can understand that when threads are of the same priority and are in the 'wait pool' waiting to be notified, there is no way we can predict which thread will get notified when notify() is called. Ofcourse notifyAll() can notify all threads.
Any responses?
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Thread priority does not play a role. Remember that threading priority and when a thread runs is vendor and system dependent. Java defers to the vendor or system on this one. So, even with Thread Prioroity of 10 versus Thread Priority of 1, you still can't be sure of which thread will be called after notify() is called. Remember that some systems are preemptive, so priority matters, but some (like windows) use time-slicing where priority does not matter.
lakshmi nair
Ranch Hand

Joined: Oct 11, 2000
Posts: 63
I am sorry for the mistake.
Once i rechecked, i found that the answer is b.
It was written as b.a,c,d which i mistaked as b,a,c,d (in a hurry ).
Sorry again for all the confusion caused.
Hope will be careful while critisizing great authors
Lakshmi
Viji Bharat
Ranch Hand

Joined: Sep 18, 2000
Posts: 101
Bill:
Going by your post, isn't choice (b) a little ambiguous? If it were a preemptive system and if thread priorities of waiting threads are different, then would the priority not matter when notify() is called?? Gee....I am confused....Please help!!!
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
B is correct because there is no way to predict which thread will get notified. If you use just notify() then the thread schedular will just notify() one of the waiting threads without regards to priority. That is why it is more common to use notifyAll() instead of notify().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads
 
Similar Threads
Logical/File Locking and nested synchronisation
notify and notifyAll
Threads question from khalid mock exam
executing Wait()
NX: URLYBird / my approach of the reading problem