Last Updated: 19 December, 2023
In Java, a thread is a lightweight sub-process; it is a small part of a process that can run independently and contains a separate path of execution. A thread uses fewer resources when it is created and exists.
Java programs are executed by the main thread. When a Java program is executed, the main thread is automatically created, and all other threads are invoked by the main thread.
In Java, multithreading is the process of executing multiple threads simultaneously for maximum utilization of the CPU. A program can be divided into a number of small processes, and every small process is represented as a single thread (a lightweight process). Multithreading allows a single program to perform two or more tasks simultaneously, increasing the application's performance.
For example, one thread is writing content on a file at the same time another thread is performing spelling checks or another different task.
Multitasking is the process of executing multiple tasks simultaneously to minimize execution time and maximize CPU utilization. In multitasking, the processes share separate memory and resources.
Multitasking can be achieved in two ways:
1. Process-based Multitasking (Multiprocessing)
Multiprocessing is mainly dependent on the number of processors (CPUs) available on the host computer. Each process initiated by the user is sent to the processor (CPU). It loads the registers on the CPU with the data related to the assigned process.
A process is heavyweight and allocates a separate memory area. When switching from one process to another, it takes longer to save and load registers, memory maps, update lists, and so on. The costs of process communication are also high.
2. Thread-based Multitasking (Multithreading)
In thread-based multitasking, a process is divided into multiple threads based on its complexity and executes multiple threads simultaneously. A thread is a lightweight sub-process that shares the same memory area. Each thread's execution is an independent process; if an exception occurs in one thread, it does not affect another thread. The cost of communication between the threads is low.
Multiprocessing | Multithreading |
---|---|
Multiprocessing is a computing technique in which two or more processors (CPUs) are used to enhance computing performance. | Multithreading creates multiple threads of a single process to increase computing power. |
Multiprocessing executes many processes simultaneously. | Multithreading executes many threads simultaneously. |
Multiprocessing allocates a separate memory area for each process. | Multithreading shares a common memory area for all the threads. |
A process is heavyweight. | A thread is lightweight. |
Communication costs between the processes are high. | Communication costs between the threads are low. |
There are two ways to create a thread in Java:
Runnable is an interface in the java.lang package. It has only one method, which is called the run() method. Any Java class that needs a thread to execute its instance should implement the Runnable interface.
Example 1: Creating a thread by implementing the Runnable Interface.
Output
Inside Runnable - run() method...
Thread is a Java class in the java.lang package. It extends the Object class and implements the Runnable interface. Thread classes provide constructors and methods to create and perform operations on threads.
Example 2: Creating a thread by extending the Thread class
Output
Thread is running...
There are mainly two types of threads in Java:
Daemon Thread
Non-Daemon Thread (User Thread)
A non-daemon thread (user thread) is a front-end thread that executes in the front-end logic of the application, such as writing content, grammar checking, etc., whereas a daemon thread runs in the background of the application and provides service to the non-daemon thread (user thread).
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What are the benefits of using multithreading in Java?
Ans. Multithreading is the process of executing multiple threads simultaneously for maximum utilization of the CPU. Multithreading allows a single program to perform two or more tasks simultaneously and increases the application's performance.
There are the following benefits of using multithreading in Java:
What is the difference between process and thread?
Ans. Process and Thread both provide an execution environment, but there are many differences between them, as given in the table below:
Process | Thread |
---|---|
An executing program is called a process. | A thread is a lightweight sub-process; it is a small part of a process that can run independently. |
A process takes longer to create and terminate. | Threads take less time to create and terminate as compared to processes. |
Process takes more time for context switching. | Thread takes less time for context switching. |
In terms of communication, process is less efficient (it takes more time). | Thread is more efficient (takes less time) in terms of communication. |
Processes are completely independent and don’t share memory. | Threads share a common memory. |
Processes require more resources. | Threads require less resources. |
Process does not share data. | Threads share data with each other. |
If one process is blocked, then it will not affect the execution of other processes. | If a user-level thread is blocked, then all other user-level threads are blocked. |
A system call is involved in it. | No system call is involved; it is created using APIs. |