Synchronization results in calmness and peace, unsynchronized is chaos.

Imagine a boy(thread 1) and a girl(thread 2) sharing an ice cream jar(synchronized code) on a sunny beach using a single spoon(object).

The synchronization is actually happening due to the single spoon, whose lock is acquired by either of the threads.

The wait() method:

When wait is called inside any thread the execution of that thread is temporarily suspended, by the JVM. Each thread that needs to call wait must declare its motive to do so which it does so by calling wait method using an object on which the thread must wait .To call the wait method this thread must be synchronized with that object, i.e., it must own an exclusive lock of that object. This lock is temporarily released if the thread calls wait().

Advertisements