A Complete Illustrated Guide to Java Concurrency for High‑Performance Architecture
This article provides a thorough, image‑rich overview of Java concurrency, covering the differences between concurrency and parallelism, thread lifecycle states, various ways to create threads, thread‑pool design and workflow, the Java Memory Model, and essential JUC utilities, all illustrated with code snippets and diagrams.
What Is Concurrent Programming?
Concurrent programming means handling multiple tasks within the same time slice by switching threads or processes, achieving pseudo‑parallelism on a single‑core CPU. In the multi‑core era, true parallelism can be realized by running tasks on different cores, which is essential for web servers, big‑data processing, real‑time systems, and other high‑throughput scenarios.
Concurrency vs. Parallelism
Concurrency (tasks A → B → C on a single CPU) interleaves execution of tasks. Parallelism (tasks A, B, C on multiple CPUs) runs tasks truly simultaneously.
Java Thread Lifecycle
A Java thread goes through six states:
NEW : thread is created.
RUNNABLE : thread is eligible to run.
RUNNING : thread is actually executing.
BLOCKED : waiting for a lock.
WAITING : waiting indefinitely (e.g., wait(), join(), park()).
TIMED_WAITING : waiting with a timeout (e.g., sleep(), join(timeout)).
TERMINATED : thread has finished execution.
Creating Threads in Java
Three common approaches are demonstrated:
1. Extending Thread
class MyThread extends Thread {
@Override
public void run() {
System.out.println("Thread execution");
}
}2. Implementing Runnable (recommended)
class Task implements Runnable {
@Override
public void run() {
// task logic
}
}3. Implementing Callable<String>
Callable<String> callable = () -> {
return "success";
};Unlike Runnable, Callable can return a value.
Thread Pools (Production‑Ready)
Thread pools reuse threads to avoid the overhead of frequent creation and destruction. The core parameters are:
corePoolSize : number of permanent (core) threads.
maximumPoolSize : maximum number of threads (core + temporary).
keepAliveTime : idle time before non‑core threads are terminated.
unit : time unit for keepAliveTime.
workQueue : blocking queue that holds pending tasks.
threadFactory : creates threads with meaningful names.
handler : rejection policy when the queue is full.
Typical creation:
ExecutorService pool = Executors.newFixedThreadPool(10);The execution workflow is:
Task submission
↓
Is core pool full?
├─ No → create a core thread
└─ Yes → enqueue task
↓
Is queue full?
├─ No → task stays in queue
└─ Yes → create a non‑core thread (if allowed) or invoke rejection handlerJava Memory Model (JMM)
JMM defines how threads interact with memory, abstracting away hardware and OS differences. It introduces:
Main Memory : shared memory visible to all threads.
Work Memory : each thread’s private copy of variables.
Threads cannot read/write main memory directly; they must operate on their work memory and then flush changes back.
Three Core Guarantees and Solutions
Visibility : changes made by one thread become visible to others. Solution: use volatile or synchronized.
Atomicity : a sequence of operations cannot be interleaved. Solution: use synchronized or J.U.C. atomic classes such as AtomicInteger.
Ordering : program order matches execution order. Solution: use volatile to insert memory barriers and enforce the happens‑before relationship.
Key Concurrency Utilities (JUC)
ReentrantLock: a re‑entrant mutual exclusion lock, alternative to synchronized with interruptible and timed lock acquisition. ReentrantReadWriteLock: separates read and write locks for read‑heavy scenarios. StampedLock: high‑performance lock introduced in Java 8, offering optimistic reads. CountDownLatch: blocks until a set number of events have occurred. CyclicBarrier: makes a group of threads wait for each other repeatedly. Semaphore: controls the number of concurrent accesses to a resource. Exchanger: swaps data between two threads. CompletableFuture: enables asynchronous programming with fluent chaining.
Illustrations
The content above equips developers with the conceptual foundations, practical code patterns, and toolset needed to design and implement robust, high‑throughput Java applications.
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.
Architect Chen
Sharing over a decade of architecture experience from Baidu, Alibaba, and Tencent.
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.
