• 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

Notify/Notifyall to be used with object or Thread?

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, I read a question in some demo exam site, I do not remember the exact question but it goes like this--
code:
--------------------------------------------------------------------------------

A thread , say Thread_A is waiting on a object bar.
How can you make sure , from any other thread, that the Thread_A leaves the waiting state?

Options were-
a. it's not possible.
b. bar.Notify()
c. bar.Notifyall()
d. Thread_A.Notify()
e. Thread_A.Notifyall()
f. Object.Notify()

--------------------------------------------------------------------------------

In K and B's thread's chapter, the Notify method is used without any prefix ..they have simple called Notify() or Notifyall() , so I am confused with above question. Please help.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prajal,

A thread , say Thread_A is waiting on a object bar.



well..

Then,

1)if one of the other threads waiting on bar is to be notified:

gotto use, bar.notify();

2)if all the threads waiting on bar have to be notified:

gotto use bar.notifyAll();

3)if from a thread you want to make Thread_A specifically to get out of the waiting state:

Not possible

===============

notify() is jus to say one of the (five threads say, a,b,c,d,e) that wait on bar should be notified... you never know what the jvm chooses to notify..
it might notify a or d or e... you just dont know and you can't specify that also.
================
so answer is:

a. it's not possible.

================


In K and B's thread's chapter, the Notify method is used without any prefix ..they have simple called Notify() or Notifyall() , so I am confused with above question. Please help.




Oh, notify or notifyAll() is still invoked on an object only...
notify(); ==> this.notify(); rite?

Regards,
Vishwa
[ January 17, 2008: Message edited by: Vishwanath Murthi ]
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two possibilities can be
b. bar.Notify() : Since your thread is waiting on bar object you can invoke bar.notify() and if you are lucky JVM will notify your thread but again i said if you are "lucky".
So to make sure your thread gets notified try below
bar.Notifyall() : Now this will notify all the threads that are waiting for bar object , Here bar object is the lock and this will make sure that your thread gets the notification.
Form javadoc:
"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."


Also
invoking notify() is equivalent to this.notify() which means the currently executing object is the lock object and invoking this code means that it will notify all the threads that are waiting on the currently executing object.
Also remember notify() , notifyAll() and wait() must always be used from synchronized code or else you will get
"java.lang.IllegalMonitorStateException": Current thread is not the owner
Form javadoc:
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:

* By executing a synchronized instance method of that object.
* By executing the body of a synchronized statement that synchronizes on the object.
* For objects of type Class, by executing a synchronized static method of that class.


The above code will throw exeception because the current thread, Main thread is not the owner of the lock object [this] because the method test() is not synchronized or notify is not invoked in a synchronized block that locks on this object.
 
reply
    Bookmark Topic Watch Topic
  • New Topic