Mastering Java Concurrency: Key Concepts, Thread Lifecycle, and JMM Explained
This article explains essential Java concurrency concepts—including synchronous vs. asynchronous execution, concurrency vs. parallelism, critical sections, blocking and non‑blocking operations—covers thread states, creation methods, priorities, common thread APIs, and the Java Memory Model’s guarantees on atomicity, visibility, and ordering.
Key Concepts
Synchronization and asynchrony describe how a method call returns: synchronous calls wait for completion, while asynchronous calls return immediately and continue execution in another thread.
Concurrency and parallelism both involve multiple tasks, but concurrency interleaves tasks (only one runs at any instant), whereas parallelism runs tasks truly simultaneously.
A critical section is a shared resource that only one thread may access at a time; without proper protection, data corruption occurs.
Blocking and non‑blocking describe thread interaction: blocking suspends a thread at the OS level, often causing context switches, while non‑blocking allows threads to proceed without waiting, assuming data integrity is maintained.
Thread States
New – thread object created.
Runnable – thread started and waiting for CPU.
Running – thread executing code.
Blocked – thread waiting for a monitor, I/O, or sleep.
Dead – thread has finished execution or terminated.
Thread Creation and Priority
Java threads can be created by extending java.lang.Thread or implementing java.lang.Runnable. After constructing a thread, invoke start() (not run()) to begin execution. Each thread has a priority (1–10) that influences scheduling but does not guarantee execution order.
Common Thread Methods
sleep()– pauses the current thread without releasing locks. join() – waits for another thread to finish. yield() – hints that the scheduler may run another thread of equal priority. interrupt() – sets the thread’s interrupt flag; if the thread is blocked in sleep(), wait(), or join(), an InterruptedException is thrown. interrupted() – checks and clears the interrupt flag.
Thread Safety
Thread safety is achieved when shared data is accessed in a way that prevents race conditions, typically using locks or other synchronization mechanisms. Without proper safety, threads may produce dirty or inconsistent data.
Java Memory Model (JMM)
The JMM defines guarantees for atomicity , visibility , and ordering of shared variables across threads.
Atomicity : reads/writes of non‑long/double fields are atomic; volatile long/double are also atomic.
Visibility : changes made by one thread become visible to others according to happens‑before relationships.
Ordering : compilers and processors may reorder instructions, but must respect data dependencies and the as‑if‑serial semantics.
Happens‑Before Rules
Program order: each action happens‑before any subsequent action in the same thread.
Monitor lock: unlocking a monitor happens‑before subsequent locking of the same monitor.
Volatile: a write to a volatile variable happens‑before any later read of that variable.
Transitivity: if A happens‑before B and B happens‑before C, then A happens‑before C.
Data Dependency and Reordering
Three dependency types—write‑after‑read, write‑after‑write, and read‑after‑write—prevent reordering that would change program results. Compilers and CPUs must preserve these dependencies.
As‑If‑Serial Semantics
Even with aggressive optimizations, a single‑threaded program must appear to execute in program order, protecting developers from unexpected reordering effects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
