Javatpoint Logo
Javatpoint Logo

How Synchronized works in Java?

Java multithreading enables the concurrent operation of several threads within a program. But when several threads use the same resources, problems like inconsistent data and racial situations can occur. Java provides synchronization techniques to solve these issues.

Synchronized Keyword

An essential component of Java's synchronization is the synchronized keyword. It can be used in code blocks and functions to regulate how many threads can access shared resources.

Synchronized Methods

A method that is marked as synchronized can only be used by one thread at a time for that specific class instance.

SynchronizedExample.java

Output:

Count Value: 200

Synchronized Blocks:

When it comes to synchronization, Java's synchronized blocks offer greater flexibility than synchronizing complete functions. We can choose which code blocks need to be run atomically and synchronize them instead of the entire procedure. Better performance is possible because just the necessary portions are synchronized, reducing the amount of time that threads compete for a lock.

Advantages of Synchronized Blocks

  1. Fine-Grained Control: By enabling you to choose synchronize only the crucial code segments, synchronized blocks reduce the amount of time that a thread is locked.
  2. Decreased Contention: We can improve concurrency and speed by reducing the contention for the lock by synchronizing only the necessary portions of a procedure.
  3. Preventing Deadlocks: By enabling threads to release the lock sooner and lowering the likelihood of cyclic dependencies, synchronized blocks can assist in preventing deadlocks.

SynchronizedExample.java

Output:

Count Value: 200

Explanation

The important portion in this example is located inside a synchronized block that employs a lock as its monitor object.

Intrinsic Locks and Reentrant Synchronization

In Java, each object has an inbuilt lock, sometimes referred to as a monitor. To synchronize access to the object's methods or code blocks, use this lock. A thread needs to acquire the intrinsic lock of the object linked to a synchronized method or block before it can begin executing it or entering a synchronized block.

The ability of a thread to reacquire the lock it already has is known as reentrant synchronization. Java allows a thread holding an object's intrinsic lock to enter any synchronized method or code block protected by that lock without encountering a block.

SynchronizedLock.java

Output:

Count Value: 0

Explanation

In this case, the increment method keeps the lock in place while calling the other synchronized function, increment.

Conclusion

It is essential to comprehend Java synchronization in order to create concurrent and thread-safe programs. In multithreaded contexts, the synchronised keyword helps manage access to shared resources, preventing race situations and inconsistent data when used to blocks or procedures.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA