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.


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.