Multithreading in java
The java programming language allows us to create a program that contains one or more parts that can run simultaneously at the same time. This type of program is known as a multithreading program. Each part of this program is called a thread. Every thread defines a separate path of execution in java. A thread is explained in different ways, and a few of them are as specified below.
A thread is a light wieght process.
A thread may also be defined as follows.
A thread is a subpart of a process that can run individually.
In java, multiple threads can run at a time, which enables the java to write multitasking programs. The multithreading is a specialized form of multitasking. All modern operating systems support multitasking. There are two types of multitasking, and they are as follows.
- Process-based multitasking
- Thread-based multitasking
It is important to know the difference between process-based and thread-based multitasking. Let's distinguish both.
Process-based multitasking | Thread-based multitasking |
---|---|
It allows the computer to run two or more programs concurrently | It allows the computer to run two or more threads concurrently |
In this process is the smallest unit. | In this thread is the smallest unit. |
Process is a larger unit. | Thread is a part of process. |
Process is heavy weight. | Thread is light weight. |
Process requires seperate address space for each. | Threads share same address space. |
Process never gain access over idle time of CPU. | Thread gain access over idle time of CPU. |
Inter process communication is expensive. | Inter thread communication is not expensive. |
In the upcoming tutorials, we learn things like, what are the different phases in a thread life cycle? how to create threads in java? and how to synchronize multiple threads?