Common Java Locks, Thread States, Synchronization, Thread Pools, and Concurrency Concepts

This article provides a comprehensive overview of Java concurrency topics, including various lock types, thread lifecycle states, differences between synchronized and Lock/ReentrantLock, thread safety, thread pool creation and customization, as well as practical multithreading use cases.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Common Java Locks, Thread States, Synchronization, Thread Pools, and Concurrency Concepts

1. Java Locks You Should Know

Optimistic lock / Pessimistic lock

Shared lock / Exclusive lock

Fair lock / Non‑fair lock

Mutex lock / Read‑Write lock

Reentrant lock

Spin lock

Segmented lock

Biased / Lightweight / Heavyweight lock

2. Java Thread States / Lifecycle

NEW : thread created but not started.

RUNNABLE : ready or running (Java combines ready and running).

BLOCKED : waiting to acquire a monitor or lock.

WAITING : waiting indefinitely for another thread.

TIMED_WAITING : waiting with a timeout.

TERMINATED : thread has completed execution.

The classic thread model includes five states: new, ready, running, waiting, and terminated; Java groups ready and running under RUNNABLE, and BLOCKED, WAITING, TIMED_WAITING are waiting‑type states.

3. Differences Between synchronized and Lock

Lock

is an interface; synchronized is a language keyword.

Exceptions in synchronized automatically release the lock; with Lock you must release it manually in a finally block. Lock can improve read‑side concurrency. synchronized can be applied to code blocks, instance methods, static methods, or classes.

Typical implementation uses ReentrantLock with lock() and unlock().

Before JDK 1.6, synchronized was less efficient; after JDK 1.6 it became comparable.

4. Differences Between synchronized and ReentrantLock

synchronized

is JVM‑implemented; ReentrantLock is a JDK library implementation. ReentrantLock offers advanced features (fairness, interruptibility) at the cost of more code.

Both are re‑entrant mutual‑exclusion locks. synchronized is always non‑fair; ReentrantLock can be configured as fair or non‑fair.

5. synchronized vs ThreadLocal

Both address concurrent access to shared variables. synchronized serializes access to a single variable. ThreadLocal provides an independent copy of a variable for each thread, allowing concurrent access without interference. ThreadLocal is simpler and yields higher concurrency than using synchronized for thread‑safety.

6. synchronized vs volatile

volatile

is a field modifier that guarantees visibility but not atomicity. synchronized provides both visibility and atomicity and can lock code blocks or methods. volatile does not block threads; synchronized may block.

7. Difference Between Thread.start() and Thread.run()

start()

creates a new thread, places it in the RUNNABLE state, and the JVM invokes run() asynchronously.

Calling run() directly executes the method in the current thread, providing no concurrency.

8. Transaction Isolation Levels and Their Issues

READ UNCOMMITTED – may cause dirty reads, non‑repeatable reads, phantom reads.

READ COMMITTED – may cause non‑repeatable reads, phantom reads.

REPEATABLE READ – may cause phantom reads.

SERIALIZABLE – prevents all these anomalies.

9. What Is Thread Safety and How Does Java Ensure It?

Thread safety means program correctness under concurrent execution.

Achieved using synchronized, explicit locks, immutable objects, etc.

10. Introduction to Thread Pools

A thread pool pre‑creates a set of worker threads to improve performance by reusing threads instead of creating and destroying them for each task.

Common factory methods in Executors: newSingleThreadExecutor: single‑thread executor. newFixedThreadPool: fixed‑size pool. newCachedThreadPool: cached pool that creates new threads as needed. newScheduledThreadPool: pool for periodic tasks.

Custom pools can be built with ThreadPoolExecutor using parameters such as corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit, BlockingQueue, ThreadFactory, and RejectedExecutionHandler.

11. Synchronous vs Asynchronous Execution

Synchronous calls wait for a response before proceeding.

Asynchronous calls return immediately, allowing other work to continue.

Synchronous mechanisms include critical sections, mutexes, semaphores, events; asynchronous improves throughput.

12. Asynchronously Obtaining Results from Multithreaded Tasks

Use Callable + Future via ExecutorService.submit().

Use Callable + FutureTask (implements both Runnable and Callable).

Use CompletionService to retrieve completed tasks as they finish.

Note: Tasks implemented with Runnable cannot return a result after execution.

13. How to Customize a Thread Pool

Instantiate ThreadPoolExecutor and configure its seven constructor arguments: corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit, BlockingQueue, ThreadFactory, and RejectedExecutionHandler.

14. Real‑World Scenarios Where Multithreading Is Used

Load testing with thousands of concurrent requests.

High‑concurrency order processing.

Bulk insertion into MySQL or Redis.

Massive data import/export with POI.

Sending millions of emails or SMS messages.

Backing up terabytes of log files.

Parallel URL validation for large datasets.

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.

ThreadPoolmultithreading
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.