Back to series
· 10 min read

Concurrency Models Across Languages


I. What Is a Concurrency Model?

A concurrency model is a mechanism provided by a programming language or runtime for organizing the execution of multiple tasks and coordinating the relationships among them.

It primarily addresses the following questions:

  • How can multiple tasks be managed at the same time?
  • How is CPU execution time allocated?
  • How is I/O waiting handled?
  • How is data passed between tasks?
  • How are shared-state conflicts avoided?
  • How is the lifecycle of a task controlled?
  • How are task cancellation, timeouts, and exceptions handled?

Concurrency and parallelism are not the same concept.

Concurrency focuses on multiple tasks making progress alternately over time, emphasizing task organization and coordination.

Parallelism focuses on multiple tasks running at the same moment using different execution resources, emphasizing actual simultaneous execution.

For example, a single-threaded event loop can handle a large number of network requests concurrently, but at any given moment usually only one JavaScript callback is executing on that thread.

Multiple CPU cores, however, can execute computational tasks in different threads in parallel.

flowchart TD
    A["Multiple tasks"] --> B{"Execution mode"}
    B --> C["Concurrency"]
    B --> D["Parallelism"]
    C --> E["Tasks progress alternately"]
    C --> F["Switch to other tasks while waiting"]
    D --> G["Multiple execution units run simultaneously"]
    D --> H["Utilize multi-core CPUs"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style H fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

1. Core Components of a Concurrency Model

A complete concurrency model usually involves the following parts:

Component Role
Execution unit Represents a task that can make progress independently, such as a thread or coroutine
Scheduler Determines when a task gets a chance to execute
Synchronization mechanism Coordinates the execution order among tasks
Communication mechanism Passes data between tasks
State management Manages data that is shared or independent across tasks
Lifecycle management Handles startup, waiting, cancellation, and termination
Exception propagation Defines how a task failure affects other tasks

Concurrency models in different languages differ mainly in how these components are implemented and what abstractions the language and runtime provide to developers.

II. Main Types of Concurrency Models

From the perspective of task organization and communication mechanisms, common concurrency models include:

  1. Thread-based concurrency model.
  2. Event-loop-based concurrency model.
  3. Coroutine-based concurrency model.
  4. Actor-based concurrency model.
  5. Message-passing-based concurrency model.
  6. Data-parallel concurrency model.

These models are not entirely mutually exclusive.

For example, Go uses goroutines, a scheduler, and channels together; Java supports threads, virtual threads, thread pools, and asynchronous tasks; Rust can combine threads, asynchronous Futures, and message channels.

III. Thread-Based Concurrency Model

A thread is a common execution unit in operating systems and runtimes.

A thread typically has its own execution stack, register context, and other execution state, while threads in the same process can share the process address space.

1. Basic Principles

In the thread-based concurrency model, a program executes different tasks by creating multiple threads.

For example:

public class Main {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            System.out.println("Task A");
        });

        Thread t2 = new Thread(() -> {
            System.out.println("Task B");
        });

        t1.start();
        t2.start();
    }
}

The operating system can schedule the two threads for execution.

If the system has multiple available CPU cores, they may also run truly in parallel.

flowchart TD
    A["Main Program"] --> B["Create Thread"]
    B --> C["Thread A"]
    B --> D["Thread B"]
    B --> E["Thread C"]
    C --> F["OS Scheduling"]
    D --> F
    E --> F
    F --> G["CPU Core Execution"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

2. Characteristics of the Thread Model

Advantages:

  • Can make full use of multi-core CPUs.
  • Suitable for CPU-intensive parallel computing.
  • Can use the thread scheduling capabilities provided by the operating system.
  • The multi-threading ecosystem is mature, with abundant tools and libraries.

Disadvantages:

  • Creating and destroying threads has a cost.
  • Thread context switching may introduce overhead.
  • Shared memory requires synchronization.
  • Race conditions, deadlocks, and data races may occur.
  • A large number of threads increases memory and scheduling pressure.

3. Shared Memory and Synchronization

Threads can usually access shared data within the same process.

For example:

class Counter {
    private int count = 0;

    public synchronized void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }
}

Here, synchronized is used to ensure that the increment operation is executed under the protection of a mutex.

If multiple threads access shared state at the same time without proper synchronization, data races or race conditions may occur.

The core difficulty of the thread model is not creating threads, but managing shared state and execution order.

4. Thread Pools

Frequently creating threads incurs extra overhead, so real-world projects usually use thread pools.

A thread pool pre-creates or creates a certain number of worker threads on demand; subsequent tasks are submitted to the thread pool and executed repeatedly by the worker threads.

flowchart TD
    A["Business Submits Task"] --> B["Task Queue"]
    B --> C["Thread Pool"]
    C --> D["Worker Thread A"]
    C --> E["Worker Thread B"]
    C --> F["Worker Thread C"]
    D --> G["Execute Task"]
    E --> G
    F --> G
    G --> H["Return Result"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

A thread pool can reduce thread creation overhead, but that does not mean the more threads, the better.

The number of threads needs to be configured based on the number of CPU cores, task type, blocking time, queue length, and resource limits.

IV. Event Loop-Based Concurrency Model

The event loop (Event Loop) is a mechanism that achieves concurrency through an event queue and task scheduling.

It usually consists of a loop that continuously checks for executable tasks and executes callbacks at the appropriate time.

JavaScript is a typical application of this model.

1. Basic Principles

The core idea of the event loop is:

  • When the current task is executing, it does not keep occupying the execution thread while waiting for ordinary asynchronous I/O.
  • Asynchronous operations are handled by the runtime or host environment.
  • After the operation completes, subsequent tasks are scheduled into the appropriate queue.
  • The event loop executes these tasks at the appropriate time.

For example:

console.log("Start");

setTimeout(() => {
    console.log("Timer");
}, 1000);

console.log("End");

Output order:

Start
End
Timer

setTimeout does not block the execution of subsequent synchronous code.

2. Basic structure of the event loop

Taking browser JavaScript as an example, the event loop involves:

  • Call stack.
  • Macrotasks and related task queues.
  • Microtask queue.
  • Asynchronous capabilities provided by the host environment.
  • Timing of rendering updates.

A simplified execution process:

flowchart TD
    A["Execute current task"] --> B["Synchronous code runs"]
    B --> C["Call async API"]
    C --> D["Host environment handles async operation"]
    D --> E["Async task completes"]
    E --> F["Schedule callback task"]
    F --> G["Current task ends"]
    G --> H["Check microtask queue"]
    H --> I["Execute microtasks"]
    I --> J["Enter next round of the event loop"]
    J --> A

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style H fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style I fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style J fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Note: this is a simplified model for easier understanding. The browser event loop, the Node.js event loop, and other JavaScript host environments are not entirely identical in terms of task queues, phases, and scheduling details.

3. Advantages of the event loop

  • Suitable for a large number of I/O-intensive tasks.
  • Reduces the management overhead of a large number of operating system threads.
  • Tasks can be organized through asynchronous callbacks.
  • Managing shared state is relatively simple.
  • Suitable for network services and interactive applications.

4. Limitations of the event loop

The event loop usually relies on a single execution thread to complete callback tasks.

If a callback performs a large amount of CPU-intensive computation, it may occupy the event loop for a long time.

For example:

function heavyTask() {
    let result = 0;

    for (let i = 0; i < 1000000000; i++) {
        result += i;
    }

    return result;
}

heavyTask();

If such tasks are executed directly in the main event loop thread, other callbacks may be unable to run in time.

Common solutions:

  • Hand off computation tasks to a Worker.
  • Use a thread pool.
  • Split the computation into multiple smaller tasks.
  • Place the computation in a separate process or service.

V. Coroutine-based concurrency model

A coroutine is a unit of program execution that can be paused and resumed.

Compared with traditional threads, coroutines usually have a lighter-weight execution context and can be scheduled by the language runtime or a coroutine library.

1. Basic principles of coroutines

Coroutines allow a function to pause during execution and resume when subsequent conditions are met.

For example, in Python:

import asyncio

async def task(name):
    print(name, "开始")
    await asyncio.sleep(1)
    print(name, "结束")

async def main():
    await asyncio.gather(
        task("A"),
        task("B")
    )

asyncio.run(main())

When both coroutines reach await, they suspend and wait.

The event loop can schedule other runnable coroutines to continue execution.

2. Scheduling process of coroutines

flowchart TD
    A["Create coroutine"] --> B["Submit to scheduler"]
    B --> C["Coroutine starts running"]
    C --> D{"Reached a suspension point?"}
    D -->|No| C
    D -->|Yes| E["Save coroutine execution state"]
    E --> F["Wait for async operation"]
    F --> G["Wait for condition to be met"]
    G --> H["Re-enter runnable queue"]
    H --> I["Resume coroutine execution"]
    I --> C

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style I fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

3. Advantages of Coroutines

  • Usually lighter weight than a large number of OS threads.
  • Can express asynchronous logic using synchronous-style code.
  • Suitable for high-concurrency I/O scenarios.
  • Can reduce explicit callback nesting.
  • Task pause and resume mechanisms are relatively flexible.

4. Limitations of Coroutines

  • Requires understanding suspension and resumption mechanisms.
  • Coroutine schedulers may add runtime complexity.
  • Blocking calls may block the underlying execution thread.
  • The number of coroutines is still limited by memory and scheduling resources.
  • Coroutine implementations vary greatly across languages.

Special note:

Coroutines being lightweight does not mean coroutines inherently have CPU parallelism.

For example, Python's asyncio usually schedules coroutines from a single event loop thread. It excels at I/O concurrency, but it does not automatically distribute CPU-intensive tasks across multiple CPU cores.

6. Actor-Based Concurrency Model

The Actor model is a concurrency model centered on independent entities.

Each Actor typically has:

  • Private state.
  • A message mailbox.
  • Message processing logic.
  • The ability to create other Actors.
  • The ability to send messages to other Actors.

Actors do not directly share mutable state; instead, they collaborate through message passing.

1. Basic Principles

Suppose an e-commerce system contains:

  • User Actor.
  • Order Actor.
  • Inventory Actor.
  • Payment Actor.

Each Actor is responsible for its own state and business logic.

For example:

User requests to place an order
Order Actor creates the order
Inventory Actor checks inventory
Payment Actor processes payment

Actors do not need to directly manipulate the internal data of other Actors; instead, they interact through messages.

2. Actor Workflow

flowchart TD
    A["External request"] --> B["Order Actor"]
    B --> C["Send inventory check message"]
    C --> D["Inventory Actor"]
    D --> E["Return inventory result"]
    E --> F["Order Actor"]
    F --> G["Send payment message"]
    G --> H["Payment Actor"]
    H --> I["Return payment result"]
    I --> J["Update order status"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style G fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style I fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style J fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

3. Advantages of the Actor Model

  • Reduce shared state issues through message passing.
  • Each Actor can independently maintain internal state.
  • Suitable for distributed systems and high-concurrency business scenarios.
  • Task failures can be handled through supervision strategies.
  • Makes it easier to split the system into independent business units.

4. Limitations of the Actor Model

  • Message passing incurs serialization and scheduling overhead.
  • Message ordering and processing guarantees require explicit design.
  • Message loss, duplication, and timeouts need to be handled.
  • Transaction coordination across Actors is relatively complex.
  • Debugging asynchronous message chains is somewhat difficult.

The Actor model does not necessarily mean there is no parallelism.

Specific Actor implementations can use multiple threads to execute different Actors, or distribute Actors across multiple processes or nodes.

VII. Concurrency Model Based on Message Passing

Message passing is a way for multiple execution units to exchange data and coordinate work.

It emphasizes completing collaboration through sending and receiving messages, rather than directly accessing shared memory.

Common implementations:

  • Channel.
  • Message queue.
  • Inter-process communication.
  • Actor mailbox.
  • Distributed messaging system.

1. Basic Patterns of Message Passing

For example, in Go:

package main

import "fmt"

func worker(ch chan string) {
    ch <- "任务完成"
}

func main() {
    ch := make(chan string)

    go worker(ch)

    result := <-ch

    fmt.Println(result)
}

Here:

  • ch <- "任务完成" indicates sending a message.
  • <-ch indicates receiving a message.
  • Channel is used to pass data between goroutines.

2. Types of Message Passing

Type Characteristics Common Uses
Synchronous message Sending and receiving require coordination Task handoff, request-response
Asynchronous message The sender does not need to wait for the receiver to finish processing Decoupling business logic, background tasks
Bounded queue Queue capacity is limited Backpressure and resource control
Unbounded queue Can theoretically receive messages continuously Requires additional control over memory growth
Point-to-point message A message is processed by one consumer Task distribution
Publish-subscribe A message can be received by multiple subscribers Event notification

3. Advantages of Message Passing

  • Reduce coupling of shared memory.
  • Make it easier to split concurrent tasks.
  • Queues can be used to buffer traffic.
  • Helpful for building distributed systems.
  • Convenient for introducing backpressure and rate limiting mechanisms.

But message passing does not mean there is no shared state at all.

Channels in the same process may pass pointers or object references; if multiple tasks still access the same mutable object, data races still need to be considered.

VIII. Go's Concurrency Model: Goroutines and Channels

Go's concurrency model is a representative example.

It combines lightweight goroutines, Channels, and a runtime scheduler.

1. Goroutine

A goroutine is a lightweight execution unit managed by the Go runtime.

Example:

package main

import (
    "fmt"
    "time"
)

func sayHello() {
    fmt.Println("Hello")
}

func main() {
    go sayHello()

    time.Sleep(time.Second)
}

Using the go keyword starts a new goroutine.

Compared with traditional operating system threads, goroutines are usually lighter to create and switch.

2. G-M-P Scheduling Model

The Go runtime uses the G-M-P model to organize goroutine scheduling.

  • G: Goroutine, representing a task to be executed.
  • M: Machine, representing an operating system thread.
  • P: Processor, representing the scheduling resources required to execute Go code.

P is responsible for maintaining runnable Gs and scheduling Gs onto M for execution.

flowchart TD
    A["Goroutine G"] --> B["P Scheduler"]
    B --> C["Local run queue"]
    C --> D["M OS thread"]
    D --> E["CPU execution"]
    F["Global run queue"] --> B
    G["Other P"] --> H["Work stealing"]
    H --> C

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style H fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px