Why must wait() always be in synchronized block

后端 未结 10 1117
傲寒
傲寒 2020-11-22 06:18

We all know that in order to invoke Object.wait(), this call must be placed in synchronized block, otherwise an IllegalMonitorStateException is thrown. But what\'s t

相关标签:
10条回答
  • 2020-11-22 06:48

    Thread wait on the monitoring object (object used by synchronization block), There can be n number of monitoring object in whole journey of a single thread. If Thread wait outside the synchronization block then there is no monitoring object and also other thread notify to access for the monitoring object, so how would the thread outside the synchronization block would know that it has been notified. This is also one of the reason that wait(), notify() and notifyAll() are in object class rather than thread class.

    Basically the monitoring object is common resource here for all the threads, and monitoring objects can only be available in synchronization block.

    class A {
       int a = 0;
      //something......
      public void add() {
       synchronization(this) {
          //this is your monitoring object and thread has to wait to gain lock on **this**
           }
      }
    
    0 讨论(0)
  • 2020-11-22 06:53

    This basically has to do with the hardware architecture (i.e. RAM and caches).

    If you don't use synchronized together with wait() or notify(), another thread could enter the same block instead of waiting for the monitor to enter it. Moreover, when e.g. accessing an array without a synchronized block, another thread may not see the changement to it...actually another thread will not see any changements to it when it already has a copy of the array in the x-level cache (a.k.a. 1st/2nd/3rd-level caches) of the thread handling CPU core.

    But synchronized blocks are only one side of the medal: If you actually access an object within a synchronized context from a non-synchronized context, the object still won't be synchronized even within a synchronized block, because it holds an own copy of the object in its cache. I wrote about this issues here: https://stackoverflow.com/a/21462631 and When a lock holds a non-final object, can the object's reference still be changed by another thread?

    Furthermore, I'm convinced that the x-level caches are responsible for most non-reproducible runtime errors. That's because the developers usually don't learn the low-level stuff, like how CPU's work or how the memory hierarchy affects the running of applications: http://en.wikipedia.org/wiki/Memory_hierarchy

    It remains a riddle why programming classes don't start with memory hierarchy and CPU architecture first. "Hello world" won't help here. ;)

    0 讨论(0)
  • 2020-11-22 06:55

    A wait() only makes sense when there is also a notify(), so it's always about communication between threads, and that needs synchronization to work correctly. One could argue that this should be implicit, but that would not really help, for the following reason:

    Semantically, you never just wait(). You need some condition to be satsified, and if it is not, you wait until it is. So what you really do is

    if(!condition){
        wait();
    }
    

    But the condition is being set by a separate thread, so in order to have this work correctly you need synchronization.

    A couple more things wrong with it, where just because your thread quit waiting doesn't mean the condition you are looking for is true:

    • You can get spurious wakeups (meaning that a thread can wake up from waiting without ever having received a notification), or

    • The condition can get set, but a third thread makes the condition false again by the time the waiting thread wakes up (and reacquires the monitor).

    To deal with these cases what you really need is always some variation of this:

    synchronized(lock){
        while(!condition){
            lock.wait();
        }
    }
    

    Better yet, don't mess with the synchronization primitives at all and work with the abstractions offered in the java.util.concurrent packages.

    0 讨论(0)
  • 2020-11-22 07:01

    as per docs:

    The current thread must own this object's monitor. The thread releases ownership of this monitor.

    wait() method simply means it releases the lock on the object. So the object will be locked only within the synchronized block/method. If thread is outside the sync block means it's not locked, if it's not locked then what would you release on the object?

    0 讨论(0)
  • 2020-11-22 07:10

    @Rollerball is right. The wait() is called, so that the thread can wait for some condition to occur when this wait() call happens, the thread is forced to give up its lock.
    To give up something, you need to own it first. Thread needs to own the lock first. Hence the need to call it inside a synchronized method/block.

    Yes, I do agree with all the above answers regarding the potential damages/inconsistencies if you did not check the condition within synchronized method/block. However as @shrini1000 has pointed out, just calling wait() within synchronized block will not avert this inconsistency from happening.

    Here is a nice read..

    0 讨论(0)
  • 2020-11-22 07:10

    We all know that wait(), notify() and notifyAll() methods are used for inter-threaded communications. To get rid of missed signal and spurious wake up problems, waiting thread always waits on some conditions. e.g.-

    boolean wasNotified = false;
    while(!wasNotified) {
        wait();
    }
    

    Then notifying thread sets wasNotified variable to true and notify.

    Every thread has their local cache so all the changes first get written there and then promoted to main memory gradually.

    Had these methods not invoked within synchronized block, the wasNotified variable would not be flushed into main memory and would be there in thread's local cache so the waiting thread will keep waiting for the signal although it was reset by notifying thread.

    To fix these types of problems, these methods are always invoked inside synchronized block which assures that when synchronized block starts then everything will be read from main memory and will be flushed into main memory before exiting the synchronized block.

    synchronized(monitor) {
        boolean wasNotified = false;
        while(!wasNotified) {
            wait();
        }
    }
    

    Thanks, hope it clarifies.

    0 讨论(0)
提交回复
热议问题