Menu

(Solved) : Modify Java Based Scheduler Multiple Queues Representing Different Priorities Example Thre Q37183706 . . .

Modify the Java-based scheduler such that it has multiple queuesrepresenting different priorities. For example, have three separatequeues, one each for priority 2, 3, and 4. Have the schedulerselect a thread from the highest-priority queue, set the thread’spriority to 5, and allow the thread to run for a time quantum. Whenthe time quantum expires, select the next thread from the highestqueue and repeat the process. You should also modify the Schedulerclass such that, when a thread is given to the scheduler, aninitial priority is specified.

public class Scheduler extends Thread{

   private CircularList queue;

   private int timeSlice;

   private static final int DEFAULT_TIME_SLICE =1000; // 1 second

  

   public Scheduler() {

      timeSlice =DEFAULT_TIME_SLICE;

      queue = newCircularList();

   }

  

   public Scheduler(int quantum) {

      timeSlice = quantum;

      queue = newCircularList();

   }

  

   /**

    * adds a thread to the queue

    * @return void

    */

   public void addThread(Thread t) {

      t.setPriority(2);

      queue.addItem(t);  

   }

  

   /**

    * this method puts the scheduler tosleep for a time quantum

    * @return void

    */

   private void schedulerSleep() {

      try {

         Thread.sleep(timeSlice);

      } catch(InterruptedException e) { };

   }

  

   public void run() {

      Thread current;

    

      // set the priority of thescheduler to the highest priority

      this.setPriority(6);

     

      while (true) {

            try{

               current= (Thread)queue.getNext();

           

               if( (current != null) && (current.isAlive()) ) {

                     current.setPriority(4);

              

                  schedulerSleep();

            System.out.println(“** * Context Switch * * * “);

              

                     current.setPriority(2);

               }

                    

            }catch (NullPointerException e3) { } ;

      }

   }

}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

public class TestScheduler {

   public static void main(String args[]) {

      /**

      * This must run at thehighest priority to ensure that

      * it can create thescheduler and the example threads.

      * If it did not run at thehighest priority, it is possible

      * that the scheduler couldpreempt this and not allow it to

      * create the examplethreads.

      */

      Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

      Scheduler CPUScheduler = newScheduler();

  

      CPUScheduler.start();

      TestThread t1 = newTestThread(“Thread 1”);

      t1.start();

      CPUScheduler.addThread(t1);

      TestThread t2 = newTestThread(“Thread 2”);

      t2.start();

      CPUScheduler.addThread(t2);

     

      TestThread t3 = newTestThread(“Thread 3”);

      t3.start();

      CPUScheduler.addThread(t3);

   }

}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

class TestThread extends Thread

{

private String name;

   public TestThread(String id) {

      name = id;

   }

  

   public void run() {

      /*

       * The thread doessomething

       **/

      while (true) {

      for (int i = 0; i <500000; i++)

            ;

      System.out.println(“I amthread ” + name);

      }

   } }

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

import java.util.*;

public class CircularList

{

   private Vector List;

   private int index;

  

   public CircularList() {

      List = new Vector(10);

      index = 0;

   }

  

   /**

    * this method returns the next elementin the list.

    * @return Object

    */

   public Object getNext() {

      Object nextElement =null;

      int lastElement;

     

      if (!List.isEmpty() ) {

         if (index== List.size() )

            index= 0;

        

         nextElement =List.elementAt(index);

        

         ++index;

      }

        

      return nextElement;

   }

  

   /**

    * this method adds an item to thelist

    * @return void

    */

   public void addItem(Object t) {

      List.addElement(t);     

   }

}

Expert Answer


Answer to Modify the Java-based scheduler such that it has multiple queues representing different priorities. For example, have th…

OR