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.
- Synchronization in java is not needed in case of immutable objects.
- Synchronized keyword is used for performing synchronization in java.
- Variables can’t be synchronized in java. It will give compile time error.
How to implement Synchronization:
- Using synchronized method.
- Using synchronized block.