Threading Model   «Prev  Next»


Lesson 2 How multithreading works
Objective Examine what multithreading is and how it works.

How Java Multithreading works

A thread[1] is a sequence of statements that is executed as part of a program. All programs have at least one thread of execution. Multithreaded programs[2] are programs that simultaneously execute two or more threads. Multithreading support is provided by the underlying operating system, which quickly switches the system's CPU (or CPUs) between running threads. The portion of the operating system that assigns threads to CPUs is the thread scheduler[3], or simply the scheduler[4].

The underlying operating system switches the system's CPUs between running threads.
The underlying operating system switches the system's CPUs between running threads.

Single Processor Systems

In single-processor systems, only one thread actually executes at a given instant. Because the thread scheduler quickly switches between running threads, it creates the appearance that multiple threads are executing simultaneously. Even though multiple threads do not execute at the same instant, we still refer to this as multithreading. In systems consisting of multiple processors, more than one thread may execute at a given instant. However, the thread scheduler still switches between multiple threads. That is because there are almost always more threads than available processors.

Difference between Context switch and Multi-threading in Java

Context switching and multi-threading are two different concepts in Java that relate to how a program executes multiple tasks concurrently. Here's a detailed comparison of the two:
  1. Multi-threading in Java: Multi-threading refers to the ability of a program to execute multiple threads simultaneously or concurrently. In Java, multi-threading is a core feature used to perform multiple operations at the same time within a single process.
    Key Points:
    • Definition: Multi-threading allows multiple threads (lightweight subprocesses) to run concurrently within the same application.
    • Implementation: In Java, you can create multiple threads by extending the `Thread` class or implementing the `Runnable` interface.

    Example:
    class MyThread extends Thread {
      public void run() {
    	  System.out.println("Thread is running");
      }
    }
    
    public class Main {
      public static void main(String[] args) {
    	  MyThread t1 = new MyThread();
    	  t1.start();
      }
    }
    

    • Purpose: Multi-threading is primarily used to improve the performance of applications by allowing multiple tasks to be performed simultaneously. For instance, it is used in server applications, parallel processing, and handling multiple client requests.
    • Concurrency: In a multi-threaded environment, threads share the same memory space but can run concurrently. Each thread can perform its task without interfering with others, although synchronization may be needed to avoid race conditions.
    • Thread Scheduling: In multi-threading, thread scheduling decides which thread gets to run at any point in time. Java relies on the underlying operating system's thread scheduling algorithms.
  2. Context Switching: Context switching refers to the process of the operating system saving the state of a currently running thread (or process) and restoring the state of a previously suspended thread (or process) to resume its execution. This happens when the CPU switches from executing one thread to another.
    Key Points:
    • Definition: Context switching is the act of saving the state (context) of a thread or process and loading the state of another thread or process. This allows the CPU to alternate between different tasks, giving the illusion of parallelism.
    • Context: The "context" includes information like the thread’s registers, program counter, memory state, and stack information. During a switch, the operating system saves this context and restores it when the thread is resumed.
    • Purpose: Context switching is necessary when multiple threads or processes share a single CPU core. The operating system ensures that all threads/processes get their turn to execute. It is fundamental to multitasking, whether through multi-threading or multi-processing.
    • Cost: Context switching introduces overhead because the CPU must save the state of the current thread and load the state of the new thread. This can reduce performance in systems with frequent context switching, as CPU cycles are used for switching rather than execution.
    • Occurrence: Context switching happens in two situations:
      • Preemptive Multitasking: The operating system interrupts a thread to switch to another based on priorities or time slicing.
      • Voluntary Switching: A thread may voluntarily yield control, or it might wait for an event (I/O, for example), leading to a context switch.


Differences Between Multi-threading and Context Switching
Feature Multi-threading Context Switching
Definition Running multiple threads concurrently within a single application Switching the CPU’s execution between threads/processes
Purpose To improve application performance by executing tasks concurrently To allow multitasking by sharing CPU time across threads
Scope Related to the application-level concept of handling multiple tasks concurrently Related to the operating system’s management of CPU time and execution
Overhead Thread management is handled by the JVM, and overhead is minimal compared to processes Context switching involves overhead because the CPU has to save and load thread states
Role of the Operating System OS manages thread scheduling; JVM handles thread execution in multi-threading OS handles context switching to allocate CPU time between threads
Control Developers control the creation and management of threads in Java applications Context switching is managed by the operating system; developers don’t control it directly
Frequency Threads may run concurrently without context switching if they are on separate CPU cores Context switching occurs whenever the CPU needs to switch between threads or processes

Summary
  • Multi-threading: Refers to the Java language feature that allows multiple threads to execute concurrently within a single application. It improves the efficiency of applications by performing multiple tasks in parallel.
  • Context Switching: Refers to the low-level operating system process where the state of a running thread is saved, and another thread is restored to run. It is a key part of how the operating system handles multitasking and allows threads to share CPU time.

In short, multi-threading is a high-level programming concept to create concurrent execution in Java, while context switching is a lower-level process managed by the operating system to enable multitasking, which may involve switching between threads in multi-threaded applications.


Program versus Process

A program is an algorithm expressed in a programming language. A process is a running instance of a program with all system resources allocated by the operating system to that instance of the program. Typically, a process consists of a 1) unique identifier, 2) a program counter, 3) executable code, 4) an address space, 5) open handles to system resources, 6) a security context as well as other elements.
A program counter[5], also called an instruction pointer, is a value maintained in the CPU register that keeps track of the instruction being executed by the CPU. It is automatically incremented at the end of the execution of an instruction. You can also think of a process as a unit of activity (or a unit of work, or a unit of execution, or a path of execution) within an operating system. The concept of process allows one computer system to support multiple units of executions.

Why multithreading

The advantage of multithreading is that it allows your programs to do more things at the same time. For example, you can have an applet that simultaneously performs a sequence of calculations while displaying the results of the calculations in a chart. Another advantage of multithreading is that it lets you maximize your programming resources. For example, one thread of a program can interact with a user, while another thread is printing output, and yet another thread is communicating over the Internet.

[1]Thread: A sequence of statements that is executed as part of a program.
[2]Multithreaded program: A program that simultaneously supports more than one thread of execution.
[3] Thread scheduler: The part of an operating system that assigns threads to CPUs. Also referred to as the scheduler.
[4] Scheduler: The part of an operating system that assigns threads to CPUs. Also referred to as the thread scheduler.
[5] program counter: A register in the control unit of the CPU that is used to keep track of the address of the current or next instruction. Typically, the program counter is advanced to the next instruction and then the current instruction is executed.

SEMrush Software