Fundamentals 29 min read

Java Synchronized Mechanism Overview

Java's synchronized keyword provides mutual exclusion by using object monitors, offering class‑method, static‑method, and block locking patterns, while the JVM optimizes performance through biased, lightweight, and heavyweight lock strategies, making it essential knowledge for efficient concurrent programming.

DeWu Technology
DeWu Technology
DeWu Technology
Java Synchronized Mechanism Overview

Java's synchronized keyword is a fundamental mechanism for thread synchronization. This article provides an in-depth analysis of its usage, implementation, and underlying principles.

1. Introduction to Synchronized

Synchronized is a Java keyword used to create mutual exclusion locks. In multithreaded environments, it ensures that only one thread can access a shared resource at a time, preventing race conditions.

2. Usage Patterns

There are three primary ways to use synchronized:

Class Methods: Applies the lock to the entire class, ensuring only one thread can execute the method at a time.

Static Methods: Locks the class object, allowing multiple threads to access different instances of the class but not the same method concurrently.

Code Blocks: Allows specifying a specific object for locking, providing fine-grained control over which resources are protected.

3. Underlying Mechanism

Synchronized relies on Java's monitor (ObjectMonitor) to manage locks. Each object has an associated monitor that tracks ownership, waiting threads, and lock states. The JVM uses CAS operations to manage monitor ownership and transitions between lock states (unlocked, biased, lightweight, and heavyweight).

4. Lock Optimization

Modern JVMs optimize synchronized through several techniques:

Biased Locking: Optimizes for single-threaded scenarios by reducing lock overhead.

Lightweight Locking: Uses CAS operations for lock acquisition and release when contention is low.

Heavyweight Locking: Falls back to OS-level locking when contention is high.

5. Code Examples

Here's a basic synchronized method implementation:

public class SynchronizedDemo {</code><code>    public synchronized void print() {</code><code>        System.out.println("Hello World");  // Only one thread can execute this at a time</code><code>    }</code><code>}

6. Conclusion

Synchronized is a versatile tool for managing thread synchronization in Java. Understanding its implementation and optimization strategies is crucial for writing efficient concurrent applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMconcurrencySynchronizationthread safety
DeWu Technology
Written by

DeWu Technology

A platform for sharing and discussing tech knowledge, guiding you toward the cloud of technology.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.