Multithreaded Programming
Multithreaded Programming
• A multithreaded program contains two or more parts that can run concurrently. Each
part of such a program is called a thread, and each thread defines a separate path of
execution.
• Thus, multithreading is a specialized form of multitasking.
• There are 2 distinct types of multitasking: process-based & thread-based.
• A process is a program that is executing. Thus, process-based multitasking is the feature
that allows your computer to run two or more programs concurrently.
• In process-based multitasking, a program is the smallest unit of code that can be
dispatched by the scheduler.
• In a thread-based multitasking environment, the thread is the smallest unit of
dispatchable code. This means that a single program can perform two or more tasks
simultaneously.
• Multitasking threads require less overhead than multitasking processes.
The Java Thread Model
• Java uses threads to reduce inefficiency by preventing the waste of CPU
cycles.
• One thread can pause without stopping other parts of your program.
• For ex: the idle time created when a thread reads data from a network or
waits for user input can be utilized elsewhere. When a thread blocks in a
Java program, only the single thread that is blocked pauses. All other
threads continue to run.
Threads exist in several states.
• A suspended thread can then be resumed, allowing it to pick up where it left off.
• At any time, a thread can be terminated, which halts its execution immediately.