Home » Java programming language

Thread Scheduler and Time Slicing in Java



Learn: What are Thread Scheduler and Time Slicing? Where, Thread Scheduler and Time Slicing are required?
Submitted by Preeti Jain, on February 09, 2018

Thread Scheduler

  • ​Thread scheduler is introduced in java for thread scheduling.
  • When we have multiple threads then which thread will get a chance is decided by thread scheduler.
  • When multiple threads are waiting then in which order the waiting threads will be executed decided by thread scheduler.
  • Thread scheduler is a part of JVM.
  • Thread scheduler schedule waiting for threads based on some priorities the threads having high priority will get a chance first.
  • We can't expect exact methodologies followed by thread scheduler. It may vary from jvm to jvm dependent . that's why we are not able to tell the exact output in multithreading concepts.
  • When multiple threads are waiting then in which order the waiting threads will be executed decided by thread scheduler.

Example:

Demonstrate the behavior of Thread Scheduler. We can't expect exact execution when we have multiple threads as we have seen in below example:

class FirstThread extends Thread{
	public void run(){
		for(int i=0;i<10;++i){
			System.out.println("I am in first thread");
			try{
				Thread.sleep(1000);
			}
			catch(InterruptedException ie){
				System.out.println("Exception occurs ");
			}
		}
	}
}

class SecondThread1{
	public static void main(String[] args){
		FirstThread ft = new FirstThread();
		ft.start();
		for(int j=1;j<10;++j){
			System.out.println("I am in second thread");
		}
	}
}

Output

D:\Java Articles>java SecondThread1
I am in second thread
I am in first thread
I am in second thread
I am in second thread
I am in second thread
I am in second thread
I am in second thread
I am in second thread
I am in second thread
I am in second thread
I am in first thread
I am in first thread
I am in first thread
I am in first thread
I am in first thread
I am in first thread
I am in first thread
I am in first thread
I am in first thread

Time Slicing

  • Time slice is introduced in operating system for scheduling processes.
  • With the help of Time slice factor short time running process will get a chance to execute.
  • A task executes for a predefined slice of time and then re-enter the pool of ready tasks.

Example:

Suppose we have two processes and one process is taking 1 and hour and second process is taking 15 minutes. And first process having high priority then thread scheduler give a chance to execute first. If we assign time slice factor of 3 minutes. Then first process will execute 3 minutes and then entered in waiting state and then after 3 minutes second process will get a chance to execute 3 minutes then back to waiting state and again first process 3 min. And go back to waiting state and so...on until completion.

The purpose of time slicing factor is only one (i.e. give a chance to execute for short time waiting process and takes a processor for sometime from long time executing processes).


Read more...

  1. Explain life cycle of a thread in java.
  2. How to Create Java Threads (java examples to create threads)?
  3. Thread Synchronisation in Java with Example.
  4. Java program to demonstrate example of thread.
  5. Java program to Join Threads.





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.