- Important in case of multi-threaded environment
- Synchronization can be of Java Object or Java Class
- Synchronized and volatile keywords are used for
synchronizing i.e. mutual exclusive access
- Concurrent access of shared objects introduces errors
like thread interference and memory consistency. To avoid these errors object
must be synchronized
- If shared object is read-only or immutable like ‘String’
then synchronization not required despite running multiple threads
- JVM guarantees java synchronized code is executed one
at a time
- synchronized keyword is related to locking and
unlocking. When entering the synchronized block or method it needs to
acquire lock so it reads from main memory rather than cache memory. When exiting
the synchronized block or method it needs to release the lock so it
flushes write operation in main memory. This eliminates memory consistency
errors.
- Any code with synchronized
method or synchronized block
is mutually exclusive
- static
synchronized method and non-static synchronized method. In
Static synchronized method class object is locked (class level lock) where
as in non-static synchronized current object (this) is locked (object
level lock). If we have a non-static and static method on same object/
scenario, they can run in parallel since one will run on class object and
other on this object.
public class Counter{private static int count = 0;public static synchronized int getCount(){ return count;}public synchoronized setCount(int count){ this.count = count; } }
- can’t have synchronized variable it gives compilation
error. So we can have volatile
variable since this reads variable from main memory rather than cache
- block synchronization preferred over method since only
the required part is made mutually exclusive since it comes at the cost of
performance
- if object used inside synchronized block is null then nullpointer exception is thrown
- java synchronized keyword is re-entrant in nature
- does not allow concurrent read. By using concept of
lock stripping and using different locks for read and write we can overcome
this limitation.
- synchronized keyword is used to limited access in only
1 JVM. If there are different JVM or different file bases or DB,
synchronized keyword does not work and we need to implement global locks.
- Can result in dead-lock or starvation in multi threading
is synchronization not properly implemented.
- Constructor can’t be synchronized
- Important methods
related to synchronization: wait(), notify(), notifyAll() which is defined
in ObjectClass
- Don’t synchronize non-final fields in synchronized
block since the value can change at any time and then different threads
may be synchronizing different objects
- String is not recommended as a lock in java
synchronized block because a string is an
immutable object and literal string and interned string
gets stored in String pool. so by any chance if any other part of the code
or any third party library used same String as there lock then they both
will be locked on the same object despite being completely unrelated which
could result in unexpected behavior and bad performance. Instead of String
object its advised to use new Object() for Synchronization in Java on
synchronized block.
private static final String LOCK = "lock"; //not recommendedprivate static final Object OBJ_LOCK = new Object(); //betterpublic void process() {synchronized(LOCK) {.......}}- Calendar and SimpleDateFormat classes are not
thread-safe and require external synchronization in multi-threaded
environment
private static final String LOCK = "lock"; //not recommendedprivate static final Object OBJ_LOCK = new Object(); //betterpublic void process() {synchronized(LOCK) {.......}}- Important in case of multi-threaded environment
Wednesday, September 14, 2016
Synchronization
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment