⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rwsem.java

📁 Reader Writer Problem to solve synchronization issues
💻 JAVA
字号:
import java.applet.*;import java.awt.*;import java.net.*;public class RWSem extends Applet {  private Semaphore[] flags = new Semaphore[5];  private Semaphore mutx = new Semaphore(1);  private Semaphore dbs = new Semaphore(1);  private Data_base database = new Data_base(1);  private static Boolean[] continuous;//  private static Integer rrc;  private static int rrc;  private Thread[] threads;  private About frame;    private Button[] step_button;  private Button[] ctrl_button;  private Button about;  private CodeWin reader_win, writer_win;  private Gui gui_win;  private Scrollbar sbar;  private Label	lab;  private int	scl;  private Image[] rwimg;  private Image[] otherimg;  private Image[] rimg;  private Image[] wimg;    static final String img_path =     "http://www.site.gmu.edu/~apolavar/java_html/kevin/";  static String[] reader_text = {    "while (TRUE) {",    "  down(mutex);",    "  ++rc;",    "  if (rc == 1)",    "    down(db);",    "  up(mutex);",    "  read_db();",    "  down(mutex);",    "  --rc;",    "  if (rc == 0);",    "    up(db);",    "  up(mutex)};",    };  static String[] writer_text = {    "while (TRUE) {",    "  create_data();",    "  down(db);",    "  write_db();",    "  up(db);",    "}"    };  public void init() {    super.init();    // Use a grid layout for the applet    setLayout(new GridLayout(2,1));    Panel pan =new Panel();    pan.setLayout(new FlowLayout(FlowLayout.CENTER));    // Create a panel to hold step buttons for the readers,    // use a grid layout for it    Panel pr = new Panel();    pr.setLayout(new GridLayout(3, 1));    // Create a panel to hold step buttons for the writers,    // use a grid layout for it    Panel pw = new Panel();    pw.setLayout(new GridLayout(2, 1));    // Create an array of buttons    step_button = new Button[5];    // A red reader "Step" button    step_button[0] = new Button("R1 Step");    step_button[0].setForeground(Color.white);    step_button[0].setBackground(Color.red);    // A green reader "Step" button    step_button[1] = new Button("R2 Step");    step_button[1].setForeground(Color.black);    step_button[1].setBackground(Color.green);    // A blue reader "Step" button    step_button[2] = new Button("R3 Step");    step_button[2].setForeground(Color.white);    step_button[2].setBackground(Color.blue);    // A red writer "Step" button    step_button[3] = new Button("W1 Step");    step_button[3].setForeground(Color.white);    step_button[3].setBackground(Color.red);    // A blue writer "Step" button    step_button[4] = new Button("W2 Step");    step_button[4].setForeground(Color.white);    step_button[4].setBackground(Color.blue);    // add the buttons to the appropriate panels    pr.add(step_button[0], 0);    pr.add(step_button[1], 1);    pr.add(step_button[2], 2);    pw.add(step_button[3], 0);    pw.add(step_button[4], 1);    // Create the main control panel, and use a grid layout for it    Panel cp = new Panel();    cp.setLayout(new FlowLayout(FlowLayout.CENTER));    // Create an array of buttons for the main control panel    ctrl_button = new Button[4];        // The "universal" step button    ctrl_button[0] = new Button("Step All");        // The go button    ctrl_button[1] = new Button("Continue");    // The stop button    ctrl_button[2] = new Button("Stop");    // The reset button    ctrl_button[3] = new Button("Reset");      /*-----------------------------------------------------*/    // The about button    about= new Button("About");          frame = new About();      /*-----------------------------------------------------*/   sbar = new Scrollbar(Scrollbar.HORIZONTAL);   sbar.setValues(10,3,5,15);   sbar.setPageIncrement(3);	Panel scrollpanel= new Panel();	scrollpanel.setLayout(new BorderLayout());   	lab=(new Label("10"));   	scrollpanel.add("Center",sbar);    // Add these buttons to the control panel    cp.add(ctrl_button[0]);    cp.add(ctrl_button[1]);    cp.add(ctrl_button[2]);    cp.add(ctrl_button[3]);    cp.add(scrollpanel);    cp.add(lab);    cp.add(new Label("Speed"));    cp.add(about);    // Load images for the reader and writer markers (in the code window)    rimg = new Image[3];    rimg[0] = getImage(getCodeBase(), "rd_ball.gif");    rimg[1] = getImage(getCodeBase(), "gr_ball.gif");    rimg[2] = getImage(getCodeBase(), "bl_ball.gif");    wimg = new Image[2];    wimg[0] = rimg[0];    wimg[1] = rimg[2];    // create a reader and writer code window    reader_win = new CodeWin(3, reader_text, rimg);    writer_win = new CodeWin(2, writer_text, wimg);   pan.add(pr);   pan.add(reader_win);   pan.add(writer_win);   pan.add(pw);   pan.add(cp);    rwimg = new Image[5];    rwimg[0] = getImage(getCodeBase(), "bookred.gif");    rwimg[1] = getImage(getCodeBase(), "bookgreen.gif");    rwimg[2] = getImage(getCodeBase(), "bookblue.gif");    rwimg[3] = getImage(getCodeBase(), "pencil_red.gif");    rwimg[4] = getImage(getCodeBase(), "pencil_blue.gif");	    otherimg = new Image[5];    otherimg[0] = getImage(getCodeBase(),"mutexunlock.gif");     otherimg[1] = getImage(getCodeBase(),"mutexlock.gif");     otherimg[2] = getImage(getCodeBase(),"dbunlock.gif");     otherimg[3] = getImage(getCodeBase(),"dblock.gif");     otherimg[4] = getImage(getCodeBase(),"textbox.gif");     gui_win = new Gui(rwimg,otherimg);   add(gui_win);   add(pan);    // Create and initialize the global rc//    rrc = new Integer(0);    rrc = 0;     // create and initialize the continuous flag    continuous = new Boolean[1];    continuous[0] = Boolean.FALSE;    // Create an array of control flags    for (int i = 0; i < 5; ++i)      flags[i] = new Semaphore(0);        // Create the reader and writer threads    threads = new Thread[5];    threads[0] = new ReaderThread(0, flags, continuous, rrc,mutx,dbs,database, reader_win,gui_win);    threads[1] = new ReaderThread(1, flags, continuous, rrc,mutx,dbs,database, reader_win,gui_win);    threads[2] = new ReaderThread(2, flags, continuous, rrc,mutx,dbs,database, reader_win,gui_win);    threads[3] = new WriterThread(3, flags, continuous, dbs,database, writer_win,gui_win);    threads[4] = new WriterThread(4, flags, continuous, dbs,database, writer_win,gui_win);    threads[0].start();    threads[1].start();    threads[2].start();    threads[3].start();    threads[4].start();  }  public boolean handleEvent(Event evt){     if(evt.id==Event.SCROLL_ABSOLUTE	|| evt.id == Event.SCROLL_LINE_DOWN	|| evt.id == Event.SCROLL_LINE_UP	|| evt.id == Event.SCROLL_PAGE_DOWN	|| evt.id == Event.SCROLL_PAGE_UP)     {     scl = ((Scrollbar)evt.target).getValue();    lab.setText(String.valueOf(scl));	gui_win.updatescale(scl);    }     return super.handleEvent(evt);   }  public boolean action(Event e, Object arg)   {        if (e.target == about)        {        if(!frame.isShowing())         {          frame.resize(300,300);          frame.show();         }        }    for (int i = 0; i < 5; ++i)       {      	if (e.target == step_button[i] &&	  continuous[0].booleanValue() == false) 	{		if(i<3) gui_win.rc_reset();		flags[i].up();		return true;        }      }    if (e.target == ctrl_button[0] &&	continuous[0].booleanValue() == false)       {      	for (int i = 0; i < 5; ++i) 	{		flags[i].up();		if(i<3) gui_win.rc_reset();        }      	return true;      }    if (e.target == ctrl_button[1] &&	continuous[0].booleanValue() == false)     {      	continuous[0] = Boolean.TRUE;      	for (int i = 0; i < 5; ++i) 	{		flags[i].up();        }      	return true;    }    if (e.target == ctrl_button[2] &&	continuous[0].booleanValue() == true) {      continuous[0] = Boolean.FALSE;      return true;    }    if (e.target == ctrl_button[3]) {      // add code to handle the reset button here	for (int i = 0; i < 5; ++i) threads[i].stop();	rrc=0;  // create and initialize the continuous flag    continuous = new Boolean[1];    continuous[0] = Boolean.FALSE; 	// Create an array of control flags	for (int i = 0; i < 5; ++i)     		flags[i]=new Semaphore(0);	dbs= new Semaphore(1);	mutx= new Semaphore(1);	database.reset();	sbar.setValues(10,3,5,15);	sbar.setPageIncrement(3);     	lab.setText("10");// Create the reader and writer threads//    threads = new Thread[5];    threads[0] = new ReaderThread(0, flags, continuous, rrc,mutx,dbs,database, reader_win,gui_win);    threads[1] = new ReaderThread(1, flags, continuous, rrc,mutx,dbs,database, reader_win,gui_win);    threads[2] = new ReaderThread(2, flags, continuous, rrc,mutx,dbs,database, reader_win,gui_win);    threads[3] = new WriterThread(3, flags, continuous, dbs,database, writer_win,gui_win);    threads[4] = new WriterThread(4, flags, continuous, dbs,database, writer_win,gui_win);     threads[0].start();    threads[1].start();    threads[2].start();    threads[3].start();    threads[4].start();	gui_win.reset();	reader_win.reset();	writer_win.reset();	      return true;    }        return false;  }  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -