Synchronization

Synchronization is a feature in java that controls the access of multiple threads to any shared resource. It is used where we want to allow only one thread to access the shared resource. It is used to prevent consistency problems and thread interference.

Implementation of Synchronization in Java is done with a concept called monitors. The monitor can be accessed by only one thread at a given time. When a thread enters the monitor, it acquires a lock on that monitor. Until the first thread exits the monitor, all other threads attempting to enter the locked monitor will be suspended until the first thread exits the monitor.

Deadlock –

Deadlock in java is a part of multi-threading. It can occur in a situation when a thread is waiting for an object lock, that is acquired by another thread, and second thread is waiting for an object lock that is acquired by the first thread. Deadlock is a condition when both threads are waiting for each other to release the lock.

There are two types of thread synchronization –
Mutual Exclusive

It helps keep threads from interfering with one another while sharing data. It can be achieved by –

  • Synchronized method –
    • This method locks an object for any shared resource.
    • When a synchronized method is invoked by a thread, it automatically acquires the lock for that object and releases it when the thread completes its task.
    • Syntax –
      • synchronized void print(object reference expression) {
      • //Access shared variables and other shared resources
      • }
  • Synchronized block –
    • It is used to perform synchronization on any specific resource of the method.
    • Suppose you have 10 lines of code in your method, and you want to synchronize only 5 lines, you can use a synchronized block.
    • Syntax –
      • synchronized (object reference expression) {   
      • //code block   
      • }
Static synchronization –
  • If we make any static method as synchronized, the lock will be on the class not on the object.
  • There will be no interference between objects of two classes.
  • Syntax –
    • synchronized static void print(object reference expression) {
    • //Access shared variables and other shared resources
    • }
Cooperation
  • It is an inter-thread communication.
  • It allows synchronized threads to communicate with each other.
  • Cooperation is a condition where a thread’s execution is paused that is running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.
  • It can be implemented with the help of the following methods of Object class:
    • wait() –
      • It causes the current thread to release the lock and wait until either a specified amount of time has elapsed or another thread notifies the object by invoking notify() or notifyAll().
      • Eg –
        • public final void wait()
        • public final void wait(long timeout)
    • notify() –
      • It wakes up one of the arbitrarily chosen thread that is waiting on this object’s monitor.
      • Eg – public final void notify()
    • notifyAll() –
      • It will wake up all the threads that have been waiting on this object’s monitor.
      • Eg – public final void notifyAll()