• 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

How is await() different from wait() ?

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All

We have the methods wait(), notify() and notifyAll() in the Object class and on the other hand we have await(), signal() and signalAll() methods in the Condition interface. How are these methods different from one another? I mean does wait() and await() solve the same purpose? Can anyone explain when each method should be used and what's the basic difference between wait() and await()?

Thanks a lot!
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swapna,

for my understanding the methods in Object and Condition basically offer the same functionality. According to the API documentation the difference is that the methods in Object are tied to the intrinsic lock of an object.
That means wait(), notify() are directly related to the one and only intrinsic lock of an instance whereas the methods in Condition can be used in combination with multiple locks. This allows you to use the functionality of wait and notify with multiple wait sets.

Marco
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marco Ehrentreich wrote:
That means wait(), notify() are directly related to the one and only intrinsic lock of an instance whereas the methods in Condition can be used in combination with multiple locks. This allows you to use the functionality of wait and notify with multiple wait sets.



The case of multiple condition vars with one lock is more common (if fact, the Lock/Condition class doesn't allow the Condition class to be used with more than one Lock).

Classic case is having a lock on a queue, with two different condition vars -- one for when the queue is empty, waiting for data, and one for when the queue is full, waiting for room.

Henry
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Henry/Marco,

What are intrinsic locks and condition variables? Could you explain me with an example so that I can understand it better. I'm not able to understand the whole concept of multiple locks, intrinsic locks and condition variables. Would you mind to explain me how these methods work or direct me to a link or a book where I can clearly understand these concepts.

Thanks!
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
obtaining instrinsic lock means the thread has acquired the monitor of the object. no thread can acquire the same montior until it is released.
if you use wait/notify, here is the code snipet

what it does is first acquire the monitor, put itself on wait for the notification, then release the monitor. And once it is notified, the thread will aquire the monitor first before it can execute methodA and methodB. and after the whole is executed and exited, the monitor is released.

for await use case;


ReentrantLock lock;
Condition notEmpty = lock.newCondition();
Condition otherCondition = lock.newCondition ();

...
lock.lock ()
try
{
notEmpty.await ();
..
}
finally
{
lock.unlock ();
}


similar sequence as wait, but you can have different conditions created for the same lock.

to notify a wait, you use notify.
to notify a condition.await, you use condition.signal.
also, most of the time, people use a spin lock during the wait to avoid race condition
hope this help.....
 
luri ron
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Classic case is having a lock on a queue, with two different condition vars -- one for when the queue is empty, waiting for data, and one for when the queue is full, waiting for room.



for performance purpose, you would have 2 different locks, one for head and one for tail. and each one has its own condition.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not necessarily. It depends on the queue's implementation and the context in which its (intended) to be used.
For instance, the LinkedBlockingQueue in the concurrent package uses the two-lock/two-condition approach, whereas the ArrayBlockingQueue also uses a two-condition approach, but based on a single lock.
 
luri ron
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
make sense. what is the need to have an array based implementation of queue?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic