Program Penjadwalan Round-Robin

Prioritas Thread
 
Java Virtual Machine memilih thread yang runnable dengan prioritas tertinggi.  Semua tread java mempunyai prioritas 1 sampai 10. Prioritas tertinggi 10 dan berakhir dengan 1 sebagai prioritas terendah. Sedangkan prioritas normal adalah 5.

Thread.MIN_PRIORITY = thread dengan prioritas terendah.
Thread.MAX_PRIORITY = thread dengan prioritas tertinggi.
Thread.NORM_PRIORITY = thread dengan prioritas normal.

Saat thread baru dibuat ia mempunyai prioritas yang sama dengan thread yang menciptakannya. Prioritas thread dapat diubah dengan menggunakan setpriority()method.

Penjadwalan Round-Robin dengan Java

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.sepriority(6);
   while(True){
      current = (Thread)queue.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 3");
            t3.start()
            CPUSchedular.addThread(t3);
      }
}
 
Autumn Falling Leaves