- Thread scheduler in java
- What is the thread scheduler in java?
- How does the thread scheduler work?
- Preemptive-priority scheduling
- First come First serve Scheduling(FIFS)
- Time-slicing scheduling
- Working with the Thread Scheduler
- Leave a Comment Cancel reply
- Thread Scheduler in Java | Time Slicing in Java
- Preemptive Scheduling
- Time-Sliced Scheduling
Thread scheduler in java
Most programmers know about multithreading in java, but few of the programmers know how a thread schedule for the exception? How JVM executes multiple threads executing concurrently? A thread scheduler schedules the thread for execution. So now the question arises that which thread should be executed first? In this post, we will find how does thread scheduler in java schedules the threads.
Here is the table content of the article that will cover this topic.
1. What is the thread scheduler in java?
2. How does thread scheduler work?
3. Preemptive-priority scheduling
4. First come First serve Scheduling(FIFS)
5. Time-slicing scheduling
6. Working with the Thread Scheduler?
What is the thread scheduler in java?
A thread scheduler in java is the part of the JVM that decides which thread should run and which should wait. The thread scheduler always chooses a thread to run only if it is in the RUNNABLE state. But there is no guarantee which thread will be chosen to run if you have multiple threads in the RUNNABLE state.
A thread scheduler has the responsibility to execute the threads. Suppose there are a number of threads, then the thread scheduler will decide which thread should be run first.
There are a number of factors or criteria that are used to select a thread.
Priority
Whenever we create a thread, it always inherits priority from its parent thread. Each thread has a priority that lies between 1 to 10. The higher priority of the thread means a higher chance to get selected for the execution. But if multiple threads have the same priority then the thread scheduler can select any random thread. We will discuss it in detail.
Arrival time
The thread scheduler also depends on the arrival time of the thread. If two or more thread has the same priority then the thread scheduler checks the arrival time of the threads.
How does the thread scheduler work?
As we know in a multithreading environment, multiple threads can run concurrently. But the CPU allocates a small amount of time(That is known as a time slice) to each thread for the execution. The thread scheduler checks each thread and takes the decision on which thread will get CPU time first. The CPU takes this decision based on several factors which we have discussed above.
The thread scheduler always chooses the thread for execution only if it is already in the RUNNABLE state. Suppose we have three threads in a program, Now let’s see how to thread scheduler is assigning CPU time to each thread.
Here CPU assigns 3 seconds to each thread and all threads work parallelly. There is some scheduling algorithm is using by thread scheduler to schedule the threads. We will discuss them below.
Preemptive-priority scheduling
As you can see the name of the algorithm already says priority scheduling. This algorithm is based on the priority of the thread. Suppose the multiple threads are in the RUNNABLE state (ready to run), the thread scheduler chooses the threads that have the highest priority. It will ignore the other’s thread and select the higher-priority thread.
The selected thread runs until a certain situation does not occur That are:
1. A higher priority thread becomes runnable,
2. When the current thread goes for stop the execution. We can use yield(), sleep(), join() method to give the chance to execute the other threads.
3. If the selected thread has completed its time slice.
If any higher priority thread comes in the RUNNABLE state or the time slice of a given thread has finished. Then the current running thread will be suspended and another thread having higher priority will get a time slice for completing its task in the Running state. This type of scheduling is called Preemptive-priority scheduling.
First come First serve Scheduling(FIFS)
According to this algorithm, the thread scheduler assigns the CPU time to the thread which comes first. The thread scheduler checks the arrival time of the thread and gives the time slice for its execution. Suppose the CPU is already executing multiple threads, meanwhile some more thread comes in the ready queue. The thread scheduler will check the arrival time of all the threads and select the thread for execution that came first.
Time-slicing scheduling
This algorithm is based on First Come First Serve and time slice. The thread scheduler assigns a piece of time to each thread which is known as a time slice. The time slice is defined in the system and every thread gets executed cyclically. Suppose there are multiple threads present in the ready queue, then the thread scheduler will assign CPU to each thread for a period. If the execution of the thread is completed within a time slice, then the thread scheduler terminates the execution and switches to another thread.
Working with the Thread Scheduler
Step 1: There are five threads having different priorities and different arrival times.
Step 2: The thread scheduler will decide which thread goes first for the CPU time.
Step 3: The thread scheduler will select the thread, that has the highest priority and starts the execution of the thread. Meanwhile, if any other thread comes with the highest priority, the then-current thread will be pre-empted from the processor, and the new thread that has the highest priority will get CPU time.
Step4: If two threads come with the same priorities (Thread2 and Thread3), then the thread scheduler uses the FCFS scheduling algorithm. The thread scheduler assigns the processor that had the first-come thread.
Leave a Comment Cancel reply
You must be logged in to post a comment.
- Basics Of Java
- What is JAVA language
- JAVA features/advantage
- How to run Java program
- Difference between JRE, JDK and JVM
- Java Virtual Machine(JVM) & JVM Architecture
- Variables in Java
- Local Variable Type Inference (Java var)
- Java control statements
- if statement in Java
- Switch statement Java
- class and object in Java
- Constructor Overloading in Java
- Constructor chaining in java
- Multiple inheritance using interface in java
- Method overloading in Java
- Method overriding in Java
- Abstract class in java
- Interface in java
- Multiple inheritance using interface in java
- Java import package
- final variable in Java
- final method in java
- final class in Java
- Static variable in java
- Static method in java
- Static block in java
- Static class in java
- Thread life cycle in java
- Thread scheduler in java
- Java Thread Sleep
- join() method in java
- yield() method in java
- wait() method in Java
- notify() method in Java
- Thread class in java
- Daemon thread in java
- Callable and Future in java
- What is immutable String in java
- What is a mutable string
- String concatenation
- String comparison in Java
- Substring in Java
- Convert string to int
- String Replace in Java
- Substring in Java
- String constructor in java
- string methods in java
- try and catch java block
- finally block in java
- throw and throws keyword in java
- Chained exception in java
- User defined exception in java
- Java try with resource
- ArrayList in java
- Java ArrayList methods
- How to add the element in ArrayList
- How to replace element in an ArrayList?
- How to remove element from arraylist java
- How to access ArrayList in java
- How to get index of object in arraylist java
- How to Iterate list in java
- How to achieve Java sort ArrayList
- How to get java sublist from ArrayList
- How to Convert list to array in java
- How to convert array to list java
- How to remove duplicates from ArrayList in java
- Difference between ArrayList and array
- Difference between ArrayList and LinkedList
- Java linked list methods
- How to do LinkedList insertion?
- How to perform deletion in linked list
- How to get the element from LinkedList
- Traverse a linked list java
- Searching in linked list
- How to convert linked list to array in java
- How to remove duplicates from linked list
- How to perform linked list sorting
- Difference between ArrayList and LinkedList
- How to add element in HashSet?
- How to remove elements from HashSet?
- TreeSet internal working
- TreeSet Methods in Java
- Internal working of HashMap
- HashMap method in java
- How to add an object in HashMap by HashMap put() method
- How to get values from hashmap in java example
- How to remove the element by Java HashMap remove() method
- How to replace the value by HashMap replace() method
- How to check the map contains key by hashmap containskey() method
- How to get java map keyset by hashmap keyset() method
- How to get java map entryset by java entryset() method
- How to iterate hashmap in java
- TreeMap(Comparator comp)
- Treemap methods
- Generic types in Java
- Advantages of generics in java
- toString() method in Java
- equals() method in java
- Java hashCode() method
- clone() method in java
- finalize() method in java
- getclass() method in java
- wait() method in Java
- notify() method in Java
- Garbage collector in java
- finalize() method in java
- equals() method in java
- Java hashCode() method
- Comparable java interface
- Comparator interface
- Difference between comparable and comparator
- Why Comparable and Comparator are useful?
- comparator() method in java
- functional interface in java 8
- Predicate in java 8
- Why predicate in java 8
- Why consumer in java 8
- How lambda expression works in java
- Java 8 lambda expression example
- Why Lambda expression use functional interface only
- Lambda expression with the return statement
- Java stream operations in java
- Intermediate operation in Java 8
- Terminal operations in java 8
- Short circuit operations in Java 8
- Lazy evaluation of stream
- Converting stream to collections and Arrays
- Java 9 module
- try with resource improvement
- Optional Class Improvements
- Private methods in interface
- Factory Methods for Immutable List, Set, Map and Map.Entry
- Jshell java 9
- Java CompletableFuture API Improvements
- Local Variable Type Inference (Java var)
- Java 11 String New Methods
Content copy is strictly prohibited. Copyright © by JavaGoal 2022. Designed & Developed By Finalrope Soft Solutions Pvt. Ltd.
Thread Scheduler in Java | Time Slicing in Java
Thread scheduler in Java is the component of JVM that determines the execution order of multiple threads on a single processor (CPU).
It decides the order in which threads should run. This process is called thread scheduling in Java.
When a system with a single processor executes a program having multiple threads, CPU executes only a single thread at a particular time.
Other threads in the program wait in Runnable state for getting the chance of execution on CPU because at a time only one thread can get the chance to access the CPU.
The thread scheduler selects a thread for execution from runnable state. But there is no guarantee that which thread from runnable pool will be selected next to run by the thread scheduler.
Java runtime system mainly uses one of the following two strategies:
Preemptive Scheduling
This scheduling is based on priority. Therefore, this scheduling is known as priority-based scheduling. In the priority-based scheduling algorithm, Thread scheduler uses the priority to decide which thread should be run.
If a thread with a higher priority exists in Runnable state (ready state), it will be scheduled to run immediately.
In case more than two threads have the same priority then CPU allocates time slots for thread execution on the basis of first-come, first-serve manner.
Time-Sliced Scheduling
The process of allocating time to threads is known as time slicing in Java. Time-slicing is based on non-priority scheduling. Under this scheduling, every running thread is executed for a fixed time period.
A currently running thread goes to the Runnable state when its time slice is elapsed and another thread gets time slots by CPU for execution.
With time-slicing, threads having lower priorities or higher priorities gets the same time slots for execution.
Hope that this tutorial has covered the basic points of time slicing and thread scheduler in Java. I hope that you will have understood this topic. In the next tutorial, we will learn thread priority.
Thanks for reading.
Next ⇒ Thread Priority in Java ⇐ PrevNext ⇒