1

Synchronized blocks in Java is a great feature when working in multiple threads, which is pretty often. I know most of how they work, but would like to be more sure of how they work when combined with wait and notify(All).

Normally when one thread enters an synchronized block, no other thread can enter this block until the first thread has left. This however is not the case when calling wait on the synchronized object. If it did, another thread would not be able to call notify(All), which requires synchronization with the object before it is called.

So does the call to wait take the call out of the synchronization? Or does java just make an exception if it finds notify(All) within a different block? Also when calling wait from one synchronized block and then notify(All) from another, does one thread wait for the other to finish before continuing, if so, which one?

Now I could setup a quick test to answer most of this, that I am aware of. But it is not going to answer the more technical stuff, which I am sure that someone here can. I am not just interested in the what and when, but also the why. Tried searching for some documented info, but could not find anything useful about wait/notify(All).

EDIT:

If others should be interested, this is the test result. If we have Thread1, Thread2 and Thread3 where the first two waits to be release and the third is the one to release them, the order will go like this.

  1. Thread1 enters and calls wait()
  2. Thread2 enters and calls wait()
  3. Thread3 enters and calls notifyAll()
  4. Thread3 finishes, always
  5. The waiting threads however has no specific order. Which one is executed first, is completly random and has nothing to do with the order in which they called wait(). The thread calling notify(All) however will always finish before any waiting threads continues.
Daniel B
  • 1,205
  • 14
  • 18
  • wait is for daemon threads that don't want to execute until another thread kicks them off –  Nov 19 '15 at 10:44
  • 1
    Wait is for any thread that needs to wait for another thread for any reason. The question was not what wait was for, but how they work technically. How Java handles them. – Daniel B Nov 19 '15 at 11:23
  • _does the call to wait take the call out of the synchronization?_ Yes. The thread releases the lock while it's inside the `wait()` call, and then re-acquires the lock before `wait()` returns. – Solomon Slow Nov 19 '15 at 13:07

1 Answers1

0

Yes, it is somehow special. wait releases the lock acquired in the synchronized block and and suspends it's thread (the thread that acquired the lock) which means other threads will be allowed to acquire the lock and modify the state. Now notify or notifyAll will wake up the thread/s that were asleep and they reacquire the lock

Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
  • Okay, so if the waiting thread re-acquired the lock when it is released from wait, at that point the thread calling notify(All) will already have a lock on the synchronized object, since notify(All) needs to be inside a synchronized block. This would mean that the thread calling notify(All) will finish before the waiting thread(s) continue? – Daniel B Nov 19 '15 at 11:20
  • @DanielB remember something, it is one thread at a time in the critical section. notifyAll will reacquire the lock that it has relinquished, from the docs:"awakened threads will not be able to proceed until the current thread relinquishes the lock on this object" – Sleiman Jneidi Nov 19 '15 at 11:27
  • Thanks, this helps. But t might be best to setup a small test to make sure that I know the exact order in which everything is executed. – Daniel B Nov 19 '15 at 11:31
  • @DanielB, Yes, the thread that called `notify()` or `notifyAll()` must leave the `synchronized` block before the `wait()` call can return. And, that was a carefully thought-out design decision. The wait/notify mechanism was meant to be used in a very specific way. http://stackoverflow.com/questions/26590542/java-lang-illegalmonitorstateexception-object-not-locked-by-thread-before-wait/26593239#26593239 – Solomon Slow Nov 19 '15 at 13:09
  • Yes I was hoping that it would be like this, but I would have liked it if the wait threads would be executed in some kind of order as well. – Daniel B Nov 19 '15 at 13:59
  • @Daniel that's bad for performance but ReentrantLock and conditions give the possibility of fair scheduling and have some other useful features. – Voo Nov 19 '15 at 21:47