javatutelearn.com

Easy Learning


Home

Search Javatutelearn.com :



Thread Scheduler

Java Virtual Machine (JVM) consists of thread scheduler. It decides which thread should be executed on the machine.

Thread Scheduler uses two types of scheduling:
(a) Pr-emptive scheduler
(b) Time slicing scheduler

(a) Preemptive scheduling: - In this scheduling, the highest priority thread executes. When it goes to waiting state or another higher priority task comes into existence, then another thread gets chance for the execution.
(b) Time slicing scheduling: - A time slice is allotted to the thread. When time slice is exhausted, then another thread gets chance for execution for its time slice.

A thread can not be started twice


A thread can not be started again, if it has been started earlier.

For Example
t1.start();
t1.start();
Here t1 thread is using start() method again. Therefore, it raises an exception IllegalThreadStateException

Example

public class test1 extends Thread {
	public void run() {
		System.out.println("Running...");
	}
	public static void main(String args[]) {
		test1 t1 = new test1();
		
		  t1.start();
		  t1.start();
	}
}

Output

Running...
Exception in thread "main" java.lang.IllegalThreadStateException at java.base/java.lang.Thread.start(Thread.java:794) at test1.main(test1.java:9)






© Copyright 2016-2024 by javatutelearn.com. All Rights Reserved.