Introduction:
Multi-threading is the concept of concurrent execution of different tasks. These days, this concept is used in many applications.
-
Music player:
Multiple tasks are done simultaneously using a thread. Thread is running to listen to the current song. Thread is used to show the list of tracks available on your device. Another thread is used for displaying ads in music players. Another thread is used for searching specific songs that you want to search. Another thread is keeping track that is there any song is added or deleted from files.
-
Text Editors:
When you are typing something, spell-checking, responding to the keyboard, formatting, etc happens at the same time or concurrently. Internally different threads are doing these tasks independently.
-
Web Browser:
Downloading the file(s) or opening multiple tabs at the same time. (consider each file and tab as a single thread). If a particular web page cannot be downloaded, that is not going to stop the web browser from downloading other web pages.
-
Computer Games:
When you play video games there is several objects like cars, bikes, humans, birds, etc. All of these are implemented as separate threads.
-
VLC Media Player:
When you open the media player, you play a song and at the same time you kept on adding new songs to the list, displaying ads, searching for a new song. All of these functions have a single thread for themselves.
-
Web Servers:
to handle multiple requests. The Thread pool is maintained. Whenever a request comes, a thread is assigned to a request.
There are other applications as well. To Understand how they work, we try to know a few basic concepts.
Thread
A thread is a lightweight sub-process, the smallest unit of processing. It is the direction or path that is taken while a program is being executed. Generally, all the programs have at least one thread, known as the main thread, which is provided by the JVM or Java Virtual Machine at the start of the program’s execution. A thread is the smallest segment of an entire process. A thread is an independent, virtual, and sequential control flow within a process. In process execution, it involves a collection of threads, and each thread shares the same memory. Each thread performs the job independently of another thread.
Lifecycle of a Thread in Java
The lifecycle of each thread in Java has five different stages. You will look into each one of those stages in detail. The Stages of the Lifecycle are mentioned below.
- New
- Runnable
- Running
- Waiting
- Dead
New: The first stage is “New”. This stage is where it initiates the thread. After that, every thread remains in the new state until the thread gets assigned to a new task.
Runnable: The next stage is the runnable stage. Here, a thread gets assigned to the task and sets itself for running the task.
Running: The third stage is the execution stage. Here, the thread gets triggered as control enters the thread, and the thread performs a task and continues the execution until it finishes the job.
Waiting: At times, there is a possibility that one process as a whole might depend on another. During such an encounter, the thread might halt for an intermediate result because of its dependency on a different process. This stage is called the Waiting Stage.
Dead: The final stage of the process execution with Multithreading in Java is thread termination. After it terminates the process, the JVM automatically declares the thread dead and terminates the thread. This stage is known as the dead thread stage.
Thread Scheduler
A component of Java that decides which thread to run or execute and which thread to wait for is called a thread scheduler in Java. In Java, a thread is only chosen by a thread scheduler if it is in the runnable state. However, if there is more than one thread in the runnable state, it is up to the thread scheduler to pick one of the threads and ignore the other ones. There are some criteria that decide which thread will execute first. There are two factors for scheduling a thread i.e. Priority and Time of arrival.
Priority: The priority of each thread lies between 1 to 10. If a thread has a higher priority, it means that the thread has a better chance of getting picked up by the thread scheduler.
Time of Arrival: Suppose two threads of the same priority enter the runnable state, then priority cannot be the factor in picking a thread from these two threads. In such a case, the arrival time of the thread is considered by the thread scheduler. A thread that arrived first gets the preference over the other threads.
Difference between Process and Thread:
S.NO | Process | Thread |
---|---|---|
1. | Process means any program is in execution. | Thread means a segment of a process. |
2. | The process takes more time to terminate. | The thread takes less time to terminate. |
3. | It takes more time to create. | It takes less time for creation. |
4. | It also takes more time for context switching. | It takes less time for context switching. |
5. | The process is less efficient in terms of communication. | Thread is more efficient in terms of communication. |
6. | Multiprogramming holds the concepts of multi-process. | We don’t need multiple programs in action for multiple threads because a single process consists of multiple threads. |
7. | The process is isolated. | Threads share memory. |
8. | The process is called the heavyweight process. | A Thread is lightweight as each thread in a process shares code, data, and resources. |
9. | Process switching uses an interface in an operating system. | Thread switching does not require calling an operating system and causes an interruption to the kernel. |
10. | 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. |
11. | The process has its own Process Control Block, Stack, and Address Space. | Thread has Parents’ PCB, its own Thread Control Block, and Stack and common Address space. |
12. | Changes to the parent process do not affect child processes. | Since all threads of the same process share address space and other resources, any changes to the main thread may affect the behavior of the other threads of the process. |
13. | A system call is involved in it. | No system call is involved, it is created using APIs. |
14. | The process does not share data with each other. | Threads share data with each other. |
Multi-Threading
Multi-threading in Java is a process of executing multiple threads simultaneously. we use multithreading rather than multiprocessing because threads use a shared memory area. They don’t allocate separate memory areas so save memory, and context-switching between the threads takes less time than process.
- Threads share the same address space.
- A thread is lightweight.
- The cost of communication between the threads is low.
Note: At least one process is required for each thread.
Advantages of Java Multithreading:
- It doesn’t block the user because threads are independent, and you can perform multiple operations at the same time.
- You can perform many operations together, so it saves time.
- Threads are independent, so it doesn’t affect other threads if an exception occurs in a single thread.
Conclusion
Many factors come under the multi-threading concept. The above factors help to understand the workings of the thread. Thus, multi-threading is used to perform different tasks simultaneously.