Write short notes on Thread Synchronization.
Answers
Thread synchronization is the concurrent execution of two or more threads that share critical resources. Threads should be synchronized to avoid critical resource use conflicts. Otherwise, conflicts may arise when parallel-running threads attempt to modify a common variable at the same time.
When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called synchronization.
For example, crediting and debiting a shared bank account concurrently amongst several users without proper discipline, will put a risk the integrity of the account data. Java provides high-level concepts for synchronization in order to control access to shared resources.
Key to synchronization is the concept of the monitor (also called a semaphore). A monitor is an object that is used as a mutually exclusive lock, or mutex. Only one thread can own a monitor at a given time.
When a thread acquires a lock, it is said to have entered the monitor. All other threads attempting to enter the locked monitor will be suspended until the first thread exits the monitor.
These other threads are said to be waiting for the monitor. A thread that owns a monitor can reenter the same monitor if it so desires.
The object lock mechanism forces the following rules of synchronization:
A thread must acquire the object lock associated with a shared resource, before it can enter the shared resource. The runtime system ensures that no other thread can enter a shared resource if another thread already holds the object lock associated with the shared resource. If a thread cannot immediately acquire the object lock, it is blocked, that is, it must wait for the lock to become available.
When a thread exits a shared resource, the runtime system ensures that the object lock is also handed over. If another thread is waiting for this object lock, it can proceed to acquire the lock in order to access to the shared resource.
The keyword ‘synchronized’ and the lock forms the basis for implementing synchronized execution of code. There are two different ways in which execution of code can be synchronized:
1) synchronized methods
2) synchronized blocks
1) Synchronized Methods
As all objects have their own implicit monitor associated with them, synchronization is easy in Java. In order to enter an object’s lock, we just need to call a method that has been modified with the keyword ‘synchronized’.
When a thread is running inside a synchronized method, all other threads that try to call it (or any other synchronized method) on the same instance they have to wait.
To exit the lock and give up control of the object to the next waiting thread, the owner of that lock simply returns from the synchronized method.
The syntax of synchronized method is as follows:
mark me as brainlist
When two or more threads needs access to a shared resource, they need some way to ensure that resource will be used only one thread in one time. The process by which this is achieved is called Synchronization.