Use of synchronization keyword

Explain the use of synchronization keyword.

- It is an essential tool in concurrent programming in Java.

- A method, declared as synchronized, first attains a lock over an object before beginning an execution. Once it has finished with the execution, whether normally or abruptly, it releases the lock achieved. In this way, a method behaves as if its body were contained in a synchronized statement.

- The main purpose of synchronization keyword is to allow one thread at a time into a particular section of code. It makes the compiler append instructions to acquire the lock on the specified object before executing the code and release it afterwards.
How do we allow one thread to wait while other to finish?
How do we allow one thread to wait while other to finish? - The wait method, notify method, and notifyAll method provide an efficient transfer of control from one thread to another...
Purpose of yield method
Purpose of yield method - The yield method causes the currently executing thread object to temporarily pause...
Difference between yielding and sleeping
Yielding and sleeping - The sleep() causes the thread to suspend its running only for a specified amount of time, The yield() causes the thread to join the ready queue in the CPU again...
Post your comment