Multithreading is multitasking, in which multiple parts of a program running concurrently. Each of those parts of programs are called thread and each thread defined different path of execution.
Unlike most other programming languages, Java provide built-in support for multithreaded programming.
When we’re discussing about multitasking, then we must discretely its two type-
- Process based
- Thread based
Process based –
This multitasking feature that allows your computer to run two or more programs concurrently. For example, on your operating system you’re using java compiler and at same time you’re using text editor or playing music or browsing at same time. In this a program is the smallest unit of code that can be dispatched by task scheduler.
Thread based –
In this multitasking feature, thread is the smallest unit of dispatchable code. That means a single program can perform two or more tasks simultaneously. For example, a text editor can format text and print at same time. Here in this operation formatting of text and printing is done by two separate threads.
Let us understand the difference between these two multitasking-
Thread based multitasking require less overhead than process based. Process based tasking requires their own separate address spaces.
Interprocess communication is expensive and limited. Context switching from one process to another is also costly. Threads, on the other hand, are lightweight. They share the same address space and cooperatively share the same heavyweight process. Interthread communication is inexpensive, and context switching from one thread to the next is low cost. While Java programs make use of process-based multitasking environments, process-based multitasking is not under the control of Java.
JAVA Thread Model:
Before we go on to understanding of JAVA Thread model, we should understand that entire JAVA run time system is depended upon threads with all class libraries designed with multithreading in mind. JAVA uses Thread to enable the entire environment to be asynchronous, which have improved the total efficiency of system by preventing wastage of CPU cycles.
To understand better how multithreading is useful, let us see how would it go if multithreading didn’t existed.
We have a scenario where we have a single threaded system. This system used the concept of event loop and polling. In this system, an event is using CPU in infinite loop and will not release until it has completed its task and meanwhile polling system will decide which event will take over the CPU next. Now the next event will block the CPU and rest other process will remain blocked for their turn. Here in this scenario we see that only one event has occupied the CPU while other events are in queue. Now if any event which has occupied the CPU and waiting for certain I/O, the wait is thus wastage of CPU time. This time could have been used by other events and enabled faster processing and improved efficiency.
These backdrops are overcome by introduction of multithreading.
Benefits of multithreading-
- This has eliminated the concept of event loop and polling, thus one thread can pause without blocking other threads of program.
- The idle time created when an thread awaits input from user or network, can be used by other thread.
- If a thread is blocked, then the single thread which is blocked pauses, rest other thread continues to run.
Thread exist in different states as mentioned below:
- Running : When thread is currently using CPU or process
- Ready to Run : Thread is ready to run as soon it gets the CPU
- Suspended : A thread temporarily suspends its activity
- Resumed : A suspended thread resumes its activity
- Blocked : A thread can be blocked when waiting for resource
- Terminated : The execution of thread is terminated, once terminated it cannot resume.
Each thread is assigned a priority by JAVA, which determines how a thread should be treated as compared to another.
Thread priorities are integer numbers which decides the relative priority of one thread to another but has no impact on speed of execution, i.e. a thread with higher priority will run at same speed as of thread with lower priority. Rather priority is used to decide which thread will execute next. This is called Context Switch.
The context Switch is decided by rules, which are explained below:
Voluntarily release Control :
A thread can voluntarily release the control by yielding, sleeping, or blocking on pending I/O. Then the highest priority thread takes control of CPU.
Preempted by a higher-priority thread :
In this scenario, a lowest priority thread which doesn’t yield, is preempted by highest priority thread, irrespective of task it is performing. Here its call of highest priority thread, if it wants to run then it’ll run.
We may come across a situation where two or more thread are having same priority, now this creates problem in deciding which thread will run first and so on. In windows 98, threads with equal priority are time slashed in round robin pattern, however in other Operating system it is the thread must voluntarily yield, until then other thread of same priority will wait.