<div dir="ltr" style="text-align: justify;" trbidi="on"><b>Features :</b></p>
<ul>
<li>The <a href="https://dineshonjava.com/2013/02/java-virtual-machine.html"><b>JVM</b> </a>schedules using a preemptive , priority based scheduling algorithm.</li>
<li>All Java threads have a priority and the thread with he highest priority is scheduled to run by the <b><a href="https://dineshonjava.com/2013/02/java-virtual-machine.html">JVM</a></b>.</li>
<li>In case two threads have the same priority a FIFO ordering is followed.</li>
</ul>
<p>
 <b>A different thread is invoked to run in case one of the following events occur:</b></p>
<p> 1.The currently running thread exits the Runnable state ie either blocks or terminates.<br />
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.</p>
<p> <b>Time Slicing is dependent on the implementation.</b></p>
<p> A thread can voluntarily yield control through the <b>yield()</b> 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.</p>
<div align='center' id="ads-id"></div>
<p><b>Thread Priorities</b><br />
<a href="https://dineshonjava.com/2013/02/java-virtual-machine.html"><b>JVM</b> </a>selects to run a Runnable thread with the highest priority.</p>
<p></p>
<ul>
<li>All Java threads have a priority in the range 1-10.</li>
<li>Top priority is 10, lowest priority is 1.Normal</li>
<li>priority ie. priority by default is 5.</li>
<li>Thread.MIN_PRIORITY &#8211; minimum thread priority</li>
<li>Thread.MAX_PRIORITY &#8211; maximum thread priority</li>
<li>Thread.NORM_PRIORITY &#8211; maximum thread priority</li>
</ul>
<p>
 Whenever a new Java thread is created it has the same priority as the thread which created it.<br />
Thread priority can be changed by the setpriority() method.</p>
<p><b>Java Based Round-Robin Scheduler</b><br />
<b>(An example)</b></p>
<pre class="highlight" name="code">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);
}
}
</pre>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/2013/04/creating-thread-in-java.html">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html">Index </a>||  ; >;>;<a href="https://dineshonjava.com/2013/04/sleeping-thread-using-sleep-method.html">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/creating-thread-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/sleeping-thread-using-sleep-method/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '482'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…