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.
|