wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads question from khalid mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads question from khalid mock exam" Watch "Threads question from khalid mock exam" New topic
Author

Threads question from khalid mock exam

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I have a doubt in threads. Please tell me which are the right answers?
Which statements concerning the methods notify() and notifyAll() are true?
a. Instances of class Thread have a method called notify()
b. A call to the method notify() will wake the thread that currently owns the monitor of the object
c. The method notify() is synchronized
d. The method notifyAll() is defined in class Thread
e. When there is more than one thread waiting to obtain monitor of an object, there is no way to be sure which thread will be notified by the notify() method.
Thank you all in advance
sasank manohar
Ranch Hand

Joined: Feb 14, 2008
Posts: 186

b and e are the right answers.
Any takers please

thanks
sdev.


"SCJP5 | SCWCD5| DEVELOPER"
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I have a doubt on (b).
I think when a thread calls wait() it releases the monitor on the object so other thread may operate on the object. Then the other thread calls notify() which will then wake up the first thread (suppose it is the only thread waiting on the monitor for the object), it will then regain the monitor on the object.
Correct me if I am wrong.
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
a and e are right.
a)true. The methods notify()and notifyAll() are defined in class Object so all classes have them.
b)false. A call to notify() wakes up a single thread that is waiting on this object's monitor. Note that the thread which is calling notify actually holds the monitor of the object, and not the waiting thread. The awoken thread will now have to compete in the usual manner to obtain a lock on the object.
c)false
d)false. notify() and notifyAll() are defined in class Object
e)true. Hence the preferred way of notifying threads is calling notifyAll() instead of notify() when more than 1 thread is waiting on the object's monitor.

[This message has been edited by Junaid Bhatra (edited August 02, 2000).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Junaid is correct. A and E are also the "correct" answers according to the exam simulator.


"I'm not back." - Bill Harding, Twister
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1850
Let me conclude
a. Instances of class Thread have a method called notify()
// correct as notify() is inherited by Thread class from Object and all subclasses will have a method called notify.
b. A call to the method notify() will wake the thread that currently owns the monitor of the object
// wrong Java API specifies - "The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object.
c. The method notify() is synchronized
// wrong notify() is declared as "public final void notify() " thus not synchronized and note that i cannot override it in my subclasses as it is final.
d. The method notifyAll() is defined in class Thread
// wrong declared in Object class
e. When there is more than one thread waiting to obtain monitor of an object, there is no way to be sure which thread will be notified by the notify() method.
// true Java - API says "The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object. "
hi Benli Xu,
your doubt on b is not tenable as the awakened thread will not be able to proceed until the current thread relinquishes the lock on this object.
Regds.
Rahul
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
Originally posted by Junaid Bhatra:
e)true. Hence the preferred way of notifying threads is calling notifyAll() instead of notify() when more than 1 thread is waiting on the object's monitor.

But..., could anyone explain for what purpose does the notify() method exists, since is allways preferred to call the notifyAll() method?
Another thing to remember is that notify(), wait(), and notifyAll() methods must be declare inside a synchronize method or block because they may throw an IllegalMonitorStateException() if, like the API doc says: a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
notify() v/s notifyAll():
If I need something to be done and any one of the "waiters"
can do it, I would wake up any one of them and let the
others sleep peacefully!
[This message has been edited by rajsim (edited August 03, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thank you all. All the explanations are very helpful to me.
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
Marcela,
notify() vs notifyAll():
notify() can be used when you are sure that only 1 thread is waiting on the object's monitor.
However if you have more than 1 thread waiting, it's always better to call notifyAll() instead. This avoids potential deadlock problems.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads question from khalid mock exam