Skip to content
Related Articles
Open in App
Not now

Related Articles

Difference Between Java Threads and OS Threads

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 31 Oct, 2022
Improve Article
Save Article

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.

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 wherever we can fit in to dwell better understanding and lastly we will be tabulating the differences between them in a tabular manner.

Java threads vs OS threads

  

Key Point OS ThreadsJava Threads
DefinitionA thread is the smallest unit of processing that can be performed in an OSA thread, in the context of Java, is the path followed when executing a program
Minimum threadsA process can contain multiple threads.Java programs have at least one thread, known as the main thread
TypesUser-level threads & Kernel-level threadsUser threads & Daemon threads.
Created/Managed byOperating SystemJava Virtual Machine (JVM) at the program’s start, when the main() method is invoked with the main thread.
CommunicationThreads 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.
SynchronizationThe most popular way of achieving thread synchronization is by using Mutexes.Implemented using monitors, synchronizing using synchronized blocks.
Implementation using modelsMany-to-One, One-to-One, Many-to-ManyGreen 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 avoidanceCan 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

Also, have a look at the different thread states:

State of Thread in Operating System

 Various states of a thread at any instant of time

Note: Green threads were abandoned in the Sun JVM for Linux as of the release of version 1.3 and native threads which are managed by OS are used. Refer to this for more details.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!