Table of Contents
Introduction
Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such a program is called a thread. So, threads are lightweight processes within a process.
To know more about Java, Download Entri App!
The primary difference is that threads within the same process run in shared memory space, while processes run in separate memory spaces. A thread is a path of execution within a process. A process can contain multiple threads. Now let us discuss the differences between java threads and OS threads. Here first we will be defining both of them alongside providing with the help of a program where ever we can fit in to dwell better understanding and lastly we will be tabulating the differences between them in a tabular manner.
On Linux, Java threads are implemented with native threads, so a Java program using threads is no different from a native program using threads. A “Java thread” is just a thread belonging to a JVM process.
On a modern Linux system (one using NPTL), all threads belonging to a process have the same process ID and parent process ID, but different thread IDs. You can see these IDs by running ps -eLf. The PID column is the process ID, the PPID column is the parent process ID, and the LWP column is the thread (LightWeight Process) ID. The “main” thread has a thread ID that’s the same as the process ID, and additional threads will have different thread ID values.
Learn to code from industry experts! Enroll here!
Older Linux systems may use the “linuxthreads” threading implementation, which is not fully POSIX-compliant, instead of NPTL. On a linuxthreads system, threads have different process IDs.
Life cycle of a Thread (Thread States)
In Java, a thread always exists in any one of the following states. These states are:
- New
- Active
- Blocked / Waiting
- Timed Waiting
- Terminated
Explanation of Different Thread States
1: What is the default value of a boolean in Java?
New: Whenever a new thread is created, it is always in the new state. For a thread in the new state, the code has not been run yet and thus has not begun its execution.
Active: When a thread invokes the start() method, it moves from the new state to the active state. The active state contains two states within it: one is runnable, and the other is running.
- Runnable: A thread, that is ready to run is then moved to the runnable state. In the runnable state, the thread may be running or may be ready to run at any given instant of time. It is the duty of the thread scheduler to provide the thread time to run, i.e., moving the thread the running state.
A program implementing multithreading acquires a fixed slice of time to each individual thread. Each and every thread runs for a short span of time and when that allocated time slice is over, the thread voluntarily gives up the CPU to the other thread, so that the other threads can also run for their slice of time. Whenever such a scenario occurs, all those threads that are willing to run, waiting for their turn to run, lie in the runnable state. In the runnable state, there is a queue where the threads lie. - Running: When the thread gets the CPU, it moves from the runnable to the running state. Generally, the most common change in the state of a thread is from runnable to running and again back to runnable.
Blocked or Waiting: Whenever a thread is inactive for a span of time (not permanently) then, either the thread is in the blocked state or is in the waiting state.
For example, a thread (let’s say its name is A) may want to print some data from the printer. However, at the same time, the other thread (let’s say its name is B) is using the printer to print some data. Therefore, thread A has to wait for thread B to use the printer. Thus, thread A is in the blocked state. A thread in the blocked state is unable to perform any execution and thus never consume any cycle of the Central Processing Unit (CPU). Hence, we can say that thread A remains idle until the thread scheduler reactivates thread A, which is in the waiting or blocked state.
When the main thread invokes the join() method then, it is said that the main thread is in the waiting state. The main thread then waits for the child threads to complete their tasks. When the child threads complete their job, a notification is sent to the main thread, which again moves the thread from waiting to the active state.
If there are a lot of threads in the waiting or blocked state, then it is the duty of the thread scheduler to determine which thread to choose and which one to reject, and the chosen thread is then given the opportunity to run.
Timed Waiting: Sometimes, waiting for leads to starvation. For example, a thread (its name is A) has entered the critical section of a code and is not willing to leave that critical section. In such a scenario, another thread (its name is B) has to wait forever, which leads to starvation. To avoid such scenario, a timed waiting state is given to thread B. Thus, thread lies in the waiting state for a specific span of time, and not forever. A real example of timed waiting is when we invoke the sleep() method on a specific thread. The sleep() method puts the thread in the timed wait state. After the time runs out, the thread wakes up and start its execution from when it has left earlier.
Terminated: A thread reaches the termination state because of the following reasons:
- When a thread has finished its job, then it exists or terminates normally.
- Abnormal termination:It occurs when some unusual events such as an unhandled exception or segmentation fault.
A terminated thread means the thread is no more in the system. In other words, the thread is dead, and there is no way one can respawn (active after kill) the dead thread.
Learn to code from industry experts! Enroll here!
Java threads vs OS threads
Key Point | OS Threads | Java Threads |
Definition | A thread is the smallest unit of processing that can be performed in an OS | A thread, in the context of Java, is the path followed when executing a program |
Minimum threads | A process can contain multiple threads. | Java programs have at least one thread, known as the main thread |
Types | User-level threads & Kernel-level threads | User threads & Daemon threads. |
Created/Managed by | Operating System | Java Virtual Machine (JVM) at the program’s start, when the main() method is invoked with the main thread. |
Communication | Threads can share common data & communication is easier. | wait(), notify(), notifyAll() are methods used for threads to communicate. |
Thread Scheduling | 1) Scheduling of user-level threads (ULT) to kernel-level threads (KLT) via Light-Weight Process (LWP) by the application developer.
2) Scheduling of kernel-level threads by the system scheduler to perform different unique os functions. |
The thread scheduler in java is the part of the JVM that decides which thread should run. Types: 1) Pre-emptive Scheduling, 2) Time Slicing. |
Synchronization | The most popular way of achieving thread synchronization is by using Mutexes. | Implemented using monitors, synchronizing using synchronized blocks. |
Implementation using models | Many-to-One, One-to-One, Many-to-Many | Green Thread model (many-to-one model), Native Thread Model (many-to-many model) |
Deadlock detection | 1) If resources have a single instance
2) If there are multiple instances of resources |
1) nested synchronized block or trying to get a lock on a different object or calling a synchronized method from another synchronized method
2) to use the io portal. It allows us to upload a thread dump and analyze it. 3) can also use jConsole or VisualVM to detect deadlock |
Deadlock avoidance | Can be done with Banker’s Algorithm. | 1) Avoid Unnecessary Locks
2) Avoid Nested Locks 3) Using Thread.join() Method 4) Use Lock Ordering 5) Lock Time-out |
To know more about Java, Download Entri App!
Why is it important to choose Entri?
- Excellent online platform for all the Competitive Exams.
- Provides updated materials created by the Entri Experts.
- Entri provides a best platform with full- length mock tests including previous year question papers.
- You can download the app for free and join the required classes.
- Entri wishes you all the best for your examinations and future endeavours.
“YOU DON’T HAVE TO BE GREAT TO START, BUT YOU HAVE TO START TO BE GREAT.”
Learn to code from industry experts! Enroll here!
Related Articles