Fundamentals 14 min read

Differences Between synchronized and Lock in Java and Their Usage

This article explains the fundamental differences between Java's synchronized keyword and the Lock interface, covering thread basics, lock types, a detailed comparison table, and practical code demos that illustrate how to acquire, try, and release locks in various scenarios.

Java Captain
Java Captain
Java Captain
Differences Between synchronized and Lock in Java and Their Usage

The author introduces the topic by recalling interview experiences that highlighted the need to understand the differences between synchronized and Lock in Java, and promises a summary of their distinctions, common pitfalls, and demo code.

Thread and Process Basics : A process contains at least one thread, and a thread is the smallest unit of execution. The article outlines important Thread methods such as start(), stop(), join(), sleep(), and run(), and clarifies that wait() and notify() belong to Object, not Thread. It also notes the difference between wait() (releases the monitor) and sleep() (does not).

Thread States : The five thread states (new, runnable, running, blocked, terminated) are illustrated with an image and described in a list.

Lock Types : The article lists re‑entrant, interruptible, fair, and read‑write locks, explaining their characteristics.

synchronized vs. Lock Comparison :

Category

synchronized

Lock

Level

Java keyword, JVM level

Regular class

Release

Automatically when the synchronized block exits or an exception occurs

Must be released explicitly in finally Acquisition

Thread A acquires, B waits; if A blocks, B continues waiting

Multiple acquisition strategies; can try to acquire without waiting indefinitely

State

Cannot be queried

Can be queried

Lock type

Re‑entrant, non‑interruptible, non‑fair

Re‑entrant, interruptible, can be fair or non‑fair

Performance

Suitable for low contention

Handles high contention better

Lock Detailed Introduction and Demo : The article presents the trimmed Lock interface source code and explains each method ( lock(), lockInterruptibly(), tryLock(), tryLock(long, TimeUnit), unlock()). It then provides two runnable examples using ReentrantLock —one demonstrating lock() and another showing tryLock() with fallback logic. The code snippets are wrapped in ... blocks.

Fair vs. Non‑fair Locks : The source of ReentrantLock is examined, showing the internal NonfairSync and FairSync classes, their lock acquisition strategies, and how the default constructor creates a non‑fair lock.

Additional Lock Mechanisms : The author supplements the discussion with low‑level details of synchronized bytecode ( monitorenter / monitorexit), lock elimination, lock coarsening, lightweight locks, and biased locks, illustrating each concept with diagrams and small code examples.

Conclusion : The author encourages using synchronized over Lock when possible, summarizes the key points, and invites readers to provide feedback.

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.

concurrencyLocksynchronizedjava-lock
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

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.