Advertisement
safriansah

Penjadwalan Round-Robin dengan Java

Jul 24th, 2018
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. public class Scheduler extends Thread{
  2.     public Scheduler(){
  3.         timeSlice = DEFAULT_TIME_SLICE;
  4.         queue = new Circularlist();
  5.     }
  6.     public Scheduler(int quantum){
  7.         timeSlice = quantum;
  8.         queue = new Circularlist();
  9.     }
  10.     public addThread(Thread t) {
  11.         t.setPriority(2);
  12.         queue.additem(t);
  13.     }
  14.     private void schedulerSleep() {
  15.         try{
  16.             Thread.sleep(timeSlice );
  17.         } catch (InterruptedException e){};
  18.     }
  19.     public void run(){
  20.         Thread current;
  21.         This.setpriority(6);
  22.         While (true) {
  23.         // get the next thread
  24.             current = (Thread)qeue.getnext();
  25.             if ( (current != null) && (current.isAlive()) ){
  26.                 current.setPriority(4);
  27.                 schedulerSleep();
  28.                 current.setPriority(2)
  29.             }
  30.         }
  31.     }  
  32.     private CircularList queue;
  33.     private int timeSlice;
  34.     private static final int DEFAULT_TIME_SLICE = 1000;
  35. }
  36. public class TesScheduler{
  37.     public static void main()String args[]) {
  38.         Thread.currentThread().setpriority(Thread.Max_Priority);
  39.         Schedular CPUSchedular = new Scheduler ();
  40.         CPUSchedular.start()
  41.         TestThread t1 = new TestThread("Thread 1");
  42.         t1.start()
  43.         CpuSchedular.addThread(t1);
  44.         TestThread t2 = new TestThread("Thread 2");
  45.         t2.start()
  46.         CpuSchedular.addThread(t2);
  47.         TestThread t3 = new TestThread("Thread 1");
  48.         t3.start()
  49.         CpuSchedular.addThread(t3);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement