Difference between Synchronized block with wait/notify and without them? Difference between Synchronized block with wait/notify and without them? multithreading multithreading

Difference between Synchronized block with wait/notify and without them?


Using synchronized makes a method / block accessible by only on thread at a time. So, yes, it's thread-safe.

The two concepts are combined, not mutually-exclusive. When you use wait() you need to own the monitor on that object. So you need to have synchronized(..) on it before that. Using .wait() makes the current thread stop until another thread calls .notify() on the object it waits on. This is an addition to synchronized, which just ensures that only one thread will enter a block/method.


So after just being embarrassed in an interview question on this I decided to look it up and understand it again for 1 billionth time.

synchronized block makes the code thread safe. No doubt about that. When wait() and notify() or notifyAll() come in is where you are trying to write more efficient code. For example, if you have a list of items that multiple threads share then if u put it in synchronized block of a monitor then threads threads will constantly jump in and run the code back and forth, back and forth during context switches......even with an empty list!

The wait() is hence used on the monitor (the object inside the synchronized(..)) as a mechanism to to tell all threads to chill out and stop using cpu cycles until further notice or notifyAll().

so something like:

synchronized(monitor) {    if( list.isEmpty() )        monitor.wait();}

...somewhere else...

synchronized(monitor){    list.add(stuff);    monitor.notifyAll();}


Making method as synchronized has two effects:

First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object

Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.

synchronization help you to guard the critical code.

If you want to establish communication between multiple threads, you have to use wait() and notify()/notifyAll()

wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

notify(): 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.

notifyAll():Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.

Simple use case for using wait() and notify() : Producer and Consumer problem.

Consumer thread has to wait till Producer thread produce data. wait() and notify() are useful in above scenario. Over a period of time, better alternatives have been introduced. Refer to this high level concurrency tutorial page.

In simple terms:

Use synchronized to guard protect critical section of your data and guard your code.

Use wait() and notify() along with synchronization if you want to establish communication between multiple threads in safe manner, which are interdependent on each other.

Related SE questions:

What does 'synchronized' mean?

A simple scenario using wait() and notify() in java