Skip to content

Multithreading Basics

Modern operating systems (Windows, macOS, Linux) support multitasking. Multitasking refers to the ability to run multiple tasks simultaneously.

Although a CPU executes instructions sequentially, even a single-core CPU can handle multiple tasks simultaneously. This is because the operating system manages multitasking by rapidly switching the CPU's attention between different tasks.

For instance, suppose you have three assignments in Chinese, Mathematics, and English, each requiring 30 minutes to complete. You can work on the Chinese assignment for 1 minute, then switch to Mathematics for 1 minute, followed by English for 1 minute.

By alternating in this manner, it may appear to some that you are completing all three assignments very quickly, giving the illusion that you are working on all three simultaneously.

Similarly, the operating system alternates between multiple tasks by allocating a small time slice (e.g., 0.001 seconds) to each application—such as letting the browser run for 0.001 seconds, then QQ (a messaging app) for 0.001 seconds, followed by the music player for 0.001 seconds. To humans, it seems as though the CPU is executing multiple tasks at the same time.

Even with multi-core CPUs, since the number of tasks typically far exceeds the number of CPU cores, tasks are still alternately executed.

Processes

In computing, a process refers to a single task or application running on a computer. Examples include a browser, a video player, a music player, and Word—each running as separate processes.

Some processes need to execute multiple sub-tasks simultaneously. For example, while using Word, you can type, perform spell checks, and print in the background simultaneously. These sub-tasks are called threads.

The relationship between processes and threads is as follows: a single process can contain one or more threads, but it must have at least one thread.

                        ┌──────────┐
                        │Process   │
                        │┌────────┐│
            ┌──────────┐││ Thread ││┌──────────┐
            │Process   ││└────────┘││Process   │
            │┌────────┐││┌────────┐││┌────────┐│
┌──────────┐││ Thread ││││ Thread ││││ Thread ││
│Process   ││└────────┘││└────────┘││└────────┘│
│┌────────┐││┌────────┐││┌────────┐││┌────────┐│
││ Thread ││││ Thread ││││ Thread ││││ Thread ││
│└────────┘││└────────┘││└────────┘││└────────┘│
└──────────┘└──────────┘└──────────┘└──────────┘
┌──────────────────────────────────────────────┐
│               Operating System               │
└──────────────────────────────────────────────┘

The smallest unit of scheduling in an operating system is actually a thread, not a process. Common operating systems like Windows and Linux use preemptive multitasking, meaning the scheduling of threads is entirely determined by the operating system. Programs cannot decide when a thread executes or for how long.

Multitasking Methods

Since the same application can have multiple processes and multiple threads within a single process, there are several methods to achieve multitasking:

Multi-Process Model (Each Process Has Only One Thread)

┌──────────┐ ┌──────────┐ ┌──────────┐
│Process   │ │Process   │ │Process   │
│┌────────┐│ │┌────────┐│ │┌────────┐│
││ Thread ││ ││ Thread ││ ││ Thread ││
│└────────┘│ │└────────┘│ │└────────┘│
└──────────┘ └──────────┘ └──────────┘

Multi-Thread Model (One Process with Multiple Threads)

┌────────────────────┐
│Process             │
│┌────────┐┌────────┐│
││ Thread ││ Thread ││
│└────────┘└────────┘│
│┌────────┐┌────────┐│
││ Thread ││ Thread ││
│└────────┘└────────┘│
└────────────────────┘

Multi-Process + Multi-Thread Model (Highest Complexity)

┌──────────┐┌──────────┐┌──────────┐
│Process   ││Process   ││Process   │
│┌────────┐││┌────────┐││┌────────┐│
││ Thread ││││ Thread ││││ Thread ││
│└────────┘││└────────┘││└────────┘│
│┌────────┐││┌────────┐││┌────────┐│
││ Thread ││││ Thread ││││ Thread ││
│└────────┘││└────────┘││└────────┘│
└──────────┘└──────────┘└──────────┘

Processes vs. Threads

Processes and threads have an inclusion relationship, but multitasking can be achieved through multi-processes, multi-threads within a single process, or a combination of both.

When deciding which method to use, consider the characteristics of processes and threads.

Disadvantages of Multi-Process Compared to Multi-Thread:

  • Higher Overhead: Creating a process incurs more overhead than creating a thread, especially in Windows systems.
  • Slower Inter-Process Communication: Communicating between processes is slower because inter-thread communication typically involves reading and writing the same variable, which is much faster.

Advantages of Multi-Process Compared to Multi-Thread:

  • Higher Stability: Multi-process applications are more stable because if one process crashes, it does not affect other processes. In contrast, if any thread crashes in a multi-threaded application, it can cause the entire process to crash.

Multithreading in Java

Java language has built-in support for multithreading: a Java program is essentially a JVM process that uses a main thread to execute the main() method. Within the main() method, multiple threads can be started. Additionally, the JVM has other background threads for tasks such as garbage collection.

Therefore, for most Java programs, when we talk about multitasking, we are referring to how to use multithreading to implement multitasking.

Characteristics of Multithreaded Programming Compared to Single-Threaded:

  • Shared Data Access: Multithreaded programming often involves reading and writing shared data and requires synchronization.

    For example, when playing a movie, one thread plays the video, another thread plays the audio. These threads must coordinate to ensure that the audio and video remain synchronized. Without proper coordination, the audio and video could become out of sync, increasing the complexity and difficulty of debugging multithreaded programs.

Features of Java Multithreading Programming:

  • Fundamental Concurrency Model: The multithreaded model is the most basic concurrency model in Java programs.
  • Dependency for Advanced Topics: Subsequent topics such as networking, databases, and web development rely on Java’s multithreaded model.

Therefore, mastering Java multithreading programming is essential for delving deeper into other subjects.

Summary

  • Multitasking: Modern operating systems support multitasking by rapidly switching the CPU's attention between multiple tasks.
  • Processes and Threads: A process can contain one or more threads. Threads are the smallest units of scheduling in an operating system.
  • Multitasking Methods:
    • Multi-Process Model: Each process has only one thread.
    • Multi-Thread Model: A single process contains multiple threads.
    • Multi-Process + Multi-Thread Model: Combines both methods for higher complexity.
  • Processes vs. Threads:
    • Multi-Process Advantages: Higher stability, as one process crashing does not affect others.
    • Multi-Process Disadvantages: Higher overhead and slower inter-process communication.
  • Multithreading in Java:
    • Java provides built-in support for multithreading.
    • Multithreaded programming involves shared data access and synchronization.
    • Multithreading is fundamental for advanced Java topics like networking and web development.
  • Importance of Mastery: Understanding and mastering multithreading is crucial for effective Java programming and for handling concurrent operations efficiently.
Multithreading Basics has loaded