Synchronization in java

Synchronization:

Synchronization is a process of controlling mutual exclusive problem in multithreading environment. Only one thread can access a resource at a particular instance of time. When a thread accesses a synchronized block or method, it acquires a lock on it and release the lock either after completion of method/block or any exception occur.
Note:

  1. Synchronization in java is not needed in case of immutable objects.
  2. Synchronized keyword is used for performing synchronization in java.
  3. Variables can’t be synchronized in java. It will give compile time error.

How to implement Synchronization:

  1. Using synchronized method.
    1. a. Using non-static synchronized method.
    2. b. static synchronized method.
  2. Using synchronized block.

 
Next Topic: Synchronized method in java with example.
Previous Topic: Inter-thread communication in java with example.