Alan Alan - 1 year ago 115
Java Question

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

If I just use synchronized, not wait/notify method, will it still keep thread-safe ?

What's the difference ?

Thx in advance.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download