- If your code is executing in a multi-threaded environment, you need synchronization for objects, which are shared among multiple threads, to avoid any corruption of state or any kind of unexpected behavior.
- Synchronization in Java will only be needed if shared object is mutable.
- JVM guarantees that Java synchronized code will only be executed by one thread at a time.
- we can not have synchronized variable in java. Using synchronized keyword with a variable is illegal and will result in compilation error. You can use java synchronized keyword only on synchronized method or synchronized block.
- we need to take care is that static synchronized method locked on class object lock and nonstatic synchronized method locks on current object (this). So it’s possible that both static and nonstatic java synchronized method running in parallel.
-
public class Counter{ private static int count = 0; public static synchronized int getCount(){ return count; } public synchoronized setCount(int count){ this.count = count; } }
- In this example of Java, the synchronization code is not properly synchronized because both getCount() and setCount() are not getting locked on the same object and can run in parallel which may result in the incorrect count.
- Whenever a thread enters into java synchronized method or blocks it acquires a lock and whenever it leaves java synchronized method or block it releases the lock. The lock is released even if thread leaves synchronized method after completion or due to any Error or Exception.
- Object level lock
- Java Thread acquires an object level lock when it enters into an instance synchronized java method
- Class level lock
- Acquires a class level lock when it enters into static synchronized java method.
- Re-entrant Lock
- if a java synchronized method calls another synchronized method which requires the same lock then the current thread which is holding lock can enter into that method without acquiring the lock.
- Locks
- One Major disadvantage of Java synchronized keyword is that it doesn't allow concurrent read, which can potentially limit scalability.
- By using the concept of lock stripping and using different locks for reading and writing, you can overcome this limitation of synchronized in Java. You will be glad to know that java.util.concurrent.locks.ReentrantReadWriteLock provides ready-made implementation of ReadWriteLock in Java.
- One more limitation of java synchronized keyword is that it can only be used to control access to a shared object within the same JVM. If you have more than one JVM and need to synchronize access to a shared file system or database, the Java synchronized keyword is not at all sufficient. You need to implement a kind of global lock for that.
- Java synchronized block is better than java synchronized method in Java because by using synchronized block you can only lock critical section of code and avoid locking the whole method which can possibly degrade performance.
- Do not synchronize on the non-final field on synchronized block in Java. because the reference of the non-final field may change anytime and then different thread might synchronizing on different objects i.e. no synchronization at all.
- Locks vs Synchronisation
- Synchronisation
- One thread at a time and other threads waiting
- Cannot do multi read even no write happening
- Cannot interrupt any thread which is waiting for acquire lock
- Cannot change priority of threads
- Locks
- Lock comes under java.util.concurrent.Lock
- One thread at a time and other threads waiting
- ReetantReadWriteLock.readLock
- ReetantReadWriteLock.writeLock
- Allows multiple reads as long as no write happens
- tryLock ->if lock available lock it
- Fairness Policy
- Longest waiting will get higher priority
- Able interrupt thread
Friday, 12 October 2018
Synchronization in Java
About Unknown
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment