[an error occurred while processing this directive]
Producer/Consumer
[an error occurred while processing this directive]
     Applet Source     All lectures

Round Robin Scheduling

   
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

class Monitor {
   Process current = null;

   // When a kicker thread enters next a Process awakens and a reference
   // to it is immediately saved in current.
   synchronized void put () {
      try { wait(); } catch (Exception e) { }
      current = (Process)(Thread.currentThread());
   }

   // When a kicker thread enters next it sets the current proccess's
   // putit to true - this causes the process to invoke the monitor's
   // put where it waits to be reawakened.
   synchronized void next () {
      if (current != null) current.putit = true;
      notify();
   }
}

// Just a plain process - it needs a hook regarding putit in order to
// be suspended
class Process extends Thread {
   CoFrame co;
   JPanel mp;
   Monitor m;
   int id, width, dot;
   boolean putit, s = false, running = true;
   double i = 0.0;

   public Process (int id, int w, CoFrame co, int d, JPanel g, Monitor m) { 
      this.m = m; 
      this.id = id; 
      this.width = w;
      this.co = co;
      this.dot = d;
      this.mp = g;
      putit = true; 
   }

   public void startIt () { s = true;  start(); }

   public void run () {
      int i = 0;
      int j = 0;
      while (i < width*10000 && running) {
         // Check to see whether this process should be suspended
         // If so, invoke the monitor's put to do it
         if (putit) {
            m.put();
            putit = false;
         }
         i++;
         if ((i+500*dot)%2000 == 0) {
            co.setDot(dot, (int)(i/10000)+30);
            co.update(mp.getGraphics());
         }
      }
   }
}

// Every 20 milliseconds this thread kicks a thread out of the wait
// state in the monitor to allow it to run.  The thread that had been
// the currently running Process is then placed back into a wait state.
class Kicker extends Thread {
   Monitor m;
   CoFrame co;
   boolean running = true;
   
   public Kicker (Monitor m, CoFrame co) { this.m = m; this.co = co; }

   public void run () {
      int proctime = 20;
      while (running) {
         try { sleep(proctime); } catch (Exception e) { }
         m.next();
         if (m.current != null) {
            // to make sure current is set by Process thread - delay here
            try { sleep(0,1); } catch (Exception e) { }
            try {
               proctime = 2*Integer.parseInt(co.txt[m.current.id].getText());
            } catch (Exception e) {
               proctime = 1;
            }
         }
      }
   }
}

class Dot {
   int left, top;
   Color color;
   String labl;
   Font fnt;

   Dot (String lbl, int l, int t, Color c, Font f) {
      labl   = lbl;
      left   = l;
      top    = t;
      color  = c;
      fnt    = f;
   }

   public void draw (Graphics q) {}
   public boolean find (int x, int y) { return false; }
}

class RectDot extends Dot {
   int width, height;

   RectDot (String s, int l, int t, int w, int h, Color c, Font f) {
      super(s,l,t,c,f);
      width = w;
      height = h;
   }
   
   public boolean find(int x, int y) {
      if (super.left < x && x < super.left + width &&
          super.top < y  && y < super.top + 2*height)
         return true;
      return false;
   }
   
   public void draw(Graphics g) {
      g.setFont(super.fnt);                
      int w = 20;
      int h = 10;
      g.setColor(color);                
      g.fillRect(left, top, width, height);                
      g.setColor(Color.black);
      g.drawString(labl, left-w/2+width/2, top+h/4+height/2);                
   }
}

class CoFrame extends JFrame implements ActionListener, AdjustmentListener {
   JButton reset, start;
   JPanel mainpanel;
   Dot dot[] = new Dot[4];
   Process proc[] = new Process[4];
   int pre[] = new int[4];
   JScrollBar scr[] = new JScrollBar[4];
   JTextField txt[] = new JTextField[4];
   Color dotcolor = Color.red;
   Font myfont = new Font("TimesRoman", Font.PLAIN, 18);
   boolean running = false;
   Monitor m = null;
   Kicker k = null;

   public CoFrame () {
      setLayout(new BorderLayout(20,20));
      JPanel q = new JPanel();
      q.setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(3,3,20,20));
      p.add(new JLabel(""));
      p.add(start = new JButton("Start"));
      p.add(new JLabel(""));
      p.add(new JLabel(""));
      p.add(reset = new JButton("Reset"));
      p.add(new JLabel(""));
      p.add(new JLabel(""));
      p.add(new JLabel(""));
      p.add(new JLabel(""));
      q.add("West", p);
      JPanel t = new JPanel();
      t.setLayout(new BorderLayout());
      JPanel r = new JPanel();
      r.setLayout(new GridLayout(4,1,10,10));
      for (int i=0 ; i < 4 ; i++) {
         r.add(scr[i] = new JScrollBar(JScrollBar.HORIZONTAL, 1, 1, 1, 11));
         scr[i].setValue(3);
         scr[i].addAdjustmentListener(this);
         pre[i] = 3;
      }
      JPanel s = new JPanel();
      s.setLayout(new GridLayout(4,1,10,10));
      for (int i=0 ; i < 4 ; i++) {
         s.add(txt[i] = new JTextField(4));
         txt[i].setEditable(false);
         txt[i].setBackground(Color.white);
         txt[i].setText(String.valueOf(pre[i]));
      }
      t.add("Center", r);
      t.add("East", s);
      q.add("Center", t);
      add("South", q);
      mainpanel = new JPanel();
      add("Center", mainpanel);
      for (int i=0; i < 4 ; i++)
         dot[i] = new RectDot("", 30, 40*i+30, 10, 25, Color.blue, myfont);
      reset.addActionListener(this);
      start.addActionListener(this);
   }

   Image offscreen;
   Dimension offscreensize;
   Graphics offgraphics;

   public synchronized void update(Graphics g) {
      Dimension d = getSize();
      if ((offscreen == null) || 
          (d.width != offscreensize.width) ||
          (d.height != offscreensize.height)) {
         offscreen = createImage(d.width, d.height);
         offscreensize = d;
         offgraphics = offscreen.getGraphics();
         offgraphics.setFont(getFont());
      }

      offgraphics.setColor(new Color(200, 230, 240));
      offgraphics.fillRect(0, 0, d.width, d.height);
      
      offgraphics.setColor(Color.black);
      offgraphics.fillRect(30, 39, d.width-60, 2);
      offgraphics.setColor(Color.black);      
      offgraphics.fillRect(30, 79, d.width-60, 2);
      offgraphics.setColor(Color.black);      
      offgraphics.fillRect(30,119, d.width-60, 2);
      offgraphics.setColor(Color.black);      
      offgraphics.fillRect(30,159, d.width-60, 2);      

      for (int i=0 ; i < 4 ; i++) dot[i].draw(offgraphics);
      g.drawImage(offscreen, 0, 0, null);
   }

   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == reset) {
         m = null;
         if (k != null) k.running = false;
         k = null;
         for (int i=0 ; i < 4 ; i++) {
            if (proc[i] != null) proc[i].running = false;
            proc[i] = null;
            dot[i].left = 30;
            pre[i] = scr[i].getValue();
         }
         update(mainpanel.getGraphics());    
      } else if (evt.getSource() == start) {
         Dimension d = getSize();
         int wid = d.width;
         m = new Monitor();
         k = new Kicker(m, this);
         k.getThreadGroup().setMaxPriority(10);
         k.setPriority(10);
         try {
            for (int i=0 ; i < 4 ; i++) {
               proc[i] = new Process(i, wid-60, this, i, mainpanel, m);
               proc[i].setPriority(scr[i].getValue());
               proc[i].startIt();
            }
         } catch (Exception e) { }
         k.start();
      }
   }

   public void adjustmentValueChanged (AdjustmentEvent evt) {
      for (int i=0; i < 4 ; i++) {
         int k = scr[i].getValue();
         if (pre[i] != k) {
            txt[i].setText(String.valueOf(k));
            try  {  
               proc[i].setPriority(k);  
            } catch (Exception e) {}
         }
         pre[i] = k;
      }
   }
   
   public void processEvent(AWTEvent evt) {
      if (evt.getID() == Event.WINDOW_DESTROY) System.exit(0);
   }
   
   public void setDot (int d, int w) { dot[d].left = w; }
}

public class RRSched extends Applet {
   public void init () {
      CoFrame co = new CoFrame();
      co.setSize(600,400);
      co.setVisible(true);
   }
}