Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

If I just use synchronized, not the wait/notify methods, will it still be thread-safe?

What's the difference?

like image 419
Alan Avatar asked Oct 20 '11 21:10

Alan


People also ask

Is it necessary to call wait and notify from synchronized block?

The wait(), notify(), and notifyAll() methods should be called for an object only when the current thread has already locked the object's lock. This point sometimes goes unnoticed because programmers are used to calling these methods from within synchronized methods or blocks. Otherwise, you will get "java.

Does notify need to be synchronized?

Without synchronization, you would have race conditions between checking the condition and actually waiting. This explains why wait() must be inside synchronized , but not notify() . Note that, for example, in the C++ API, this is explicitly discouraged.

What is difference between synchronized and synchronized block?

Key DifferencesA synchronized method assigns an object-level or class-level corresponding lock. And synchronized block assign a lock to the object based on the parameter.

Why wait () notify () and notifyAll () methods have to be called from synchronized method or block illustrate with an example?

Calling notify() or notifyAll() methods issues a notification to a single or multiple threads that a condition has changed and once the notification thread leaves the synchronized block, all the threads which are waiting for fight for object lock on which they are waiting and lucky thread returns from wait() method ...


2 Answers

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.

like image 196
Bozho Avatar answered Oct 28 '22 22:10

Bozho


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(); } 
like image 37
Arash Sharif Avatar answered Oct 29 '22 00:10

Arash Sharif