Java Multithreaded Programming
A Thread in Java is an execution context or a lightweight process. It is a single sequential flow of control within a program. Programmer may use Java Thread mechanism to execute multiple tasks at the same time (i.e. multitasking). Simply put, a thread is an independent path of execution within a program. Most programs of today run as a single thread, thereby causing problems when multiple events or actions need to occur at the same time. Let’s say, for example, a program is not capable of drawing pictures while reading keystrokes. The program must give its full attention to the keyboard input lacking the ability to handle more than one event at a time. The ideal solution to this problem is the seamless execution of two or more sections of a program at the same time. Threads allow us to do this.
A Thread is an independent sequential path of execution within a program, called a lightweight process. A Java program can have many threads, and these threads can run concurrently, either asynchronously or synchronously. At runtime, threads in a program exist in a common memory space and can, therefore, share both data and code, that is why they are lightweight compared to processes.
⇒ A Thread is an independent sequential path of execution within a program, called a lightweight process.
Multithreading refers to two or more tasks executing concurrently within a single program. Nearly all operating systems are capable of multitasking by using one of the two multitasking techniques: process-based multitasking and thread-based multitasking.
- Process-based multitasking is running two or more processes concurrently. Programmers refer to a program as a process. Therefore, we could say that process-based multitasking is program-based multitasking. A familiar example is running the spreadsheet program while also working with the word-processor.
- Thread-based multitasking is having a program perform two or more tasks at the same time. For example, a word processing program can check the spelling of words in a document while we write the document. This is thread-based multitasking.
The objective of multitasking is to maximize the CPU utilization. We want the CPU cycles to be processing instructions and data rather than waiting for something to process. Therefore the operating system must do extra work to manage multitasking. Programmers call this extra work overhead because in multitasking resources inside our computer are used to manage the multitasking operation rather than being used by programs for processing instructions and data.
⇒ Multithreading refers to two or more tasks executing concurrently within a single program.
Difference between two techniques
A good way to remember the difference between process-based multitasking and thread-based multitasking is to think of process-based multitasking as working with multiple programs to run concurrently on the computer and thread-based as working with parts of one program to run concurrently on the computer.
Process-based multitasking has a larger overhead than thread-based multitasking. In process-based multitasking, each process requires its own address space in memory. The operating system requires a significant amount of CPU time to switch from one process to another process. Programmers call this context switching, where each process (program) is a context. Additional resources are needed for each process to communicate with each other.
In comparison, the threads in thread-based multitasking share the same address space in memory because they share the same program. This also has an impact on context switching, because switching from one part of the program to another happens within the same address space in memory. Likewise, communication among parts of the program happens within the same memory location.
In summary the advantages of thread-based multitasking as compared to process-based multitasking are:
- Threads are lightweight compared to processes
- Threads share the same address space and therefore can share both data and code
- Context switching between threads is usually less expensive than between processes
- Cost of communication between threads is relatively low
- Threads allow different tasks to be performed concurrently