Thread Scheduling in Java

Features :
  • The JVM schedules using a preemptive , priority based scheduling algorithm.
  • All Java threads have a priority and the thread with he highest priority is scheduled to run by the JVM.
  • In case two threads have the same priority a FIFO ordering is followed.

A different thread is invoked to run in case one of the following events occur:

1.The currently running thread exits the Runnable state ie either blocks or terminates.
2. A thread with a higher priority than the thread currently running enters the Runnable state. The lower priority thread is preempted and the higher priority thread is scheduled to run.

Time Slicing is dependent on the implementation.

A thread can voluntarily yield control through the yield() method. Whenever a thread yields control of the CPU another thread of the same priority is scheduled to run. A thread voluntarily yielding control of the CPU is called Cooperative Multitasking.

Thread Priorities
JVM selects to run a Runnable thread with the highest priority.

  • All Java threads have a priority in the range 1-10.
  • Top priority is 10, lowest priority is 1.Normal
  • priority ie. priority by default is 5.
  • Thread.MIN_PRIORITY – minimum thread priority
  • Thread.MAX_PRIORITY – maximum thread priority
  • Thread.NORM_PRIORITY – maximum thread priority

Whenever a new Java thread is created it has the same priority as the thread which created it.
Thread priority can be changed by the setpriority() method.

Java Based Round-Robin Scheduler
(An example)

public class Scheduler extends Thread

{
public Scheduler(){
timeSlice = DEFAULT_TIME_SLICE;
queue = new Circularlist();
}

public Scheduler(int quantum){
timeSlice = quantum;
queue = new Circularlist();
}

public addThread(Thread t) {
t.setPriority(2);
queue.additem(t);
}

private void schedulerSleep() {
try{
Thread.sleep(timeSlice );
} catch (InterruptedException e){};
}
public void run(){
Thread current;

This.setpriority(6);
While (true) {
// get the next thread
current = (Thread)qeue.getnext();
if ( (current != null) && (current.isAlive()) ){
current.setPriority(4);
schedulerSleep();
current.setPriority(2)
}
}
}

private CircularList queue;
private int timeSlice;
private static final int DEFAULT_TIME_SLICE = 1000;
}
public class TesScheduler
{
public static void main()String args[]) {
Thread.currentThread().setpriority(Thread.Max_Priority);
Schedular CPUSchedular = new Scheduler ();
CPUSchedular.start()
TestThread t1 = new TestThread("Thread 1");
t1.start()
CpuSchedular.addThread(t1);
TestThread t2 = new TestThread("Thread 2");
t2.start()
CpuSchedular.addThread(t2);
TestThread t3 = new TestThread("Thread 1");
t3.start()
CpuSchedular.addThread(t3);
}
}

<<Previous <<   || Index ||   >>Next >>

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago