• Post Reply Bookmark Topic Watch Topic
  • New Topic

why a wait call should be from a synchronized block?  RSS feed

 
kamal krishna bhatt
Ranch Hand
Posts: 41
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why a wait call should be from a synchronized block?
can you explain with example?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because its waiting to be notified. The thread wont move until its notified.

Say you have two threads. 1 thread to call notify, one thread to call wait. Now say one thread calls notify BEFORE the other thread calls wait. Well then the notify is used. Now you have a second thread stuck on wait, but the other thread already called notify.

So your second thread missed its call. You need to synchronize to ensure you don't miss your call.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kamal: Think about it in reverse order. Question: What happens when the wait()
ends? Answer: The waiting thread recaptures an object lock and then continues its
work. So 'synchronized' must be used earlier to identify the locked object; the lock
that is released during the wait() and recaptured later.

Jim ...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, every object has a lock that is associated with it.

When you synchronize on an object, then the running thread is acquiring the lock. Only one thread at a time can hold the lock of an object. If another thread tries to enter the synchronized block and the lock is already held by another thread, then that thread must wait until the other thread releases the lock.

You can only call wait() on an object if the current thread is holding the lock for that object. What happens when you call wait() is that the thread temporarily releases the lock, until another thread calls notify() on the object. After that happens, wait() returns and the thread will once again acquire the lock.

Oracle's Concurrency Tutorial explains this in a lot of detail.
 
kamal krishna bhatt
Ranch Hand
Posts: 41
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gilbert i think according to you why we should call wait() in synchronized block is the thread calling notify should not execute before thread calling wait(),but synchronization ensures resource holding not scheduling.how can we say which thread - calling wait or notify, will hit first. can you give me an example.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
synchronization is used for thread communication. and wait and notify/notifyAll are used by threads on an object to communicate. hence those call must be with in a synchronized block.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!