• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads question from khalid mock exam

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

b and e are the right answers.
Any takers please

thanks
sdev.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junaid is correct. A and E are also the "correct" answers according to the exam simulator.
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. All the explanations are very helpful to me.
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic