Fundamentals 9 min read

Understanding Concurrency and Parallelism in Java Multithreading

This article introduces the basics of Java multithreading concurrency, explains the difference between concurrency and parallelism with a supermarket analogy, and details thread pool creation, usage, and customization through analysis of ThreadPoolExecutor source code.

FunTester
FunTester
FunTester
Understanding Concurrency and Parallelism in Java Multithreading

This chapter introduces the fundamentals of Java multithreading concurrency, starting with common implementations and gradually exploring two typical thread pool creation methods, their suitable scenarios, and the key parameters for customizing a Java thread pool by analyzing the source code of java.util.concurrent.ThreadPoolExecutor .

Concurrency and Parallelism

Before diving into Java multithreading programming, it is essential to understand the concepts of concurrency and parallelism.

These concepts are rarely distinguished deliberately in practice, but they form the basic knowledge required for Java multithreading.

Concurrency and parallelism both refer to handling multiple tasks at the same time, yet they differ: parallelism means tasks truly run simultaneously, while concurrency may involve interleaved execution. From an observer's perspective, concurrency can appear similar to parallelism.

To illustrate, consider a supermarket checkout scenario.

Imagine a supermarket called "Xiao Ba" with a single checkout counter but two checkout lanes. When traffic is light, only one lane is open; during busy periods, both lanes are opened to speed up checkout and reduce waiting time.

In reality, there is only one cashier, and the checkout counter is a black box to customers, who cannot see that only one cashier is processing both lanes.

Each customer's checkout process consists of scanning items, calculating the amount, paying, and packing.

When the store is idle, a checkout takes about one minute per customer. When both lanes are open, the average checkout time remains about one minute, seemingly doubling performance.

However, the single cashier alternates between the two lanes: waiting for a payment code from the first lane while scanning items for the second lane, and so on, as shown in Figure 1‑1.

Figure 1‑1 Concurrency at the checkout.

In this analogy, the supermarket represents the computer, the checkout counter or cashier represents the CPU, and the lanes represent threads. Adding a second lane doubles throughput, similar to increasing concurrency.

If viewed from the customer's perspective, both lanes processing checkout simultaneously is called concurrency. If the store hires a second cashier, each handles a lane independently, which is parallelism, as illustrated in Figure 1‑2.

Figure 1‑2 Parallelism at the checkout.

For CPUs, programs are analogous to customers, so performance testing in Java focuses more on concurrency.

Concurrency and parallelism are closely related but distinct terms in computer science, used to describe how tasks are executed.

Key characteristics summary:

Feature

Concurrency

Parallelism

Definition

Multiple tasks execute alternately, emphasizing task switching and coordination.

Multiple tasks execute simultaneously, emphasizing simultaneity and leveraging multi‑core or multi‑processor resources.

Execution Unit

Tasks can run on a single or multiple cores via time‑slicing (time sharing).

Tasks must run concurrently on multiple cores, threads, or processors.

Characteristics

- Emphasizes logical task structure (tasks can be partially completed).

- Focuses on program design to avoid race conditions.

- Emphasizes hardware capability, requiring hardware support for simultaneous execution.

- Increases task throughput.

Typical Scenarios

- Multi‑task handling, e.g., GUI where UI responds while background data updates.

- Asynchronous I/O operations.

- Scientific computing such as large matrix calculations, image processing.

- Parallel data processing like MapReduce, GPU computing.

Hardware Requirements

Does not require multiple cores; can be achieved in a multithreaded environment.

Requires multiple cores, processors, or GPU.

Technical Examples

- Java thread pools (e.g., Executor).

- Go's Goroutines.

- CUDA GPU programming.

- OpenMP multithreaded computing.

Key Issues

Designing alternating task logic, avoiding deadlocks and resource contention.

Allocating tasks to multiple compute units to maximize hardware utilization.

FunTester Original Essence Chaos Engineering, Fault Testing, Web Frontend Server-side Functional Testing Performance Testing Topics Java, Groovy, Go White-box, Tools, Crawlers, UI Automation Theory, Insights, Videos
JavaconcurrencymultithreadingParallelismThreadPoolExecutor
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login 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.