Synchronization in java
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.
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.
- a. Using non-static synchronized method.
- 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.