The Synchronization of Threads


Threads add a non-deterministic element into program execution which can lead to unexpected output, even corruption of data. Unfortunately, the wait()-notify(), as presented does not provide sufficient support for the programmer. Indeed, as presented, it adds additional complications.
Consider this linked example.

Question: What happens, if the first Thread to run finishes its time-slice after notify() but before wait()?

Is it possible for both threads to go into a wait state, a deadlock?

The answer is no! The reason is that the key word synchronized.

The rules of synchronization are as follows: