Synchronization in Java
Synchronization in Java
Synchronization in Java
1.Process Synchronization
2.Thread Synchronization
• Here, we will discuss only thread synchronization.
Thread Synchronization
1.Mutual Exclusive
1.Synchronized method.
2.Synchronized block.
3.Static synchronization.
2.Cooperation (Inter-thread communication in java)
Mutual Exclusive
• Mutual Exclusive helps keep threads from interfering with one another
while sharing data.
• Every object has a lock associated with it. By convention, a thread that
needs consistent access to an object's fields has to acquire the object's
lock before accessing them, and then release the lock when it's done
with them.
• If we put all the codes of the method in the synchronized block, it will
work same as the synchronized method.
Continuee..(Points to Remember)
• Synchronized block is used to lock an object for any shared resource.
• Scope of synchronized block is smaller than the method.
• A Java synchronized block doesn't allow more than one JVM, to
provide access control to a shared resource.
• The system performance may degrade because of the slower working
of synchronized keyword.
• Java synchronized block is more efficient than Java synchronized
method.
Syntax