/* * cs101 Graphical Boolean semaphore utility * $Id: GBS.java,v 1.3 2003/09/23 15:36:21 gus Exp $ * * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. * For more information, see the * CS101 homepage or email . * * Copyright (C) 1996 Massachusetts Institute of Technology. * Please do not redistribute without obtaining permission. */ package cs101.util.semaphore; import java.awt.*; import cs101.awt.ColorField; /** * Implements simple binary semaphores in java witha graphical display.
* Interface is gbs.request(), gbs.release().
* It also provides a graphical display of the semaphores status.
*

* This utility borrows heavily from the design and implementation * of cs101.util.BS. It mearly adds a graphical display. *
* Copyright 1996 Massachusetts Institute of Technology * * @author Todd C. Parnell, tparnell@ai.mit.edu * @author Joshua R. Brown, reuben@ai.mit.edu (added graphics) * @author Lynn Andrea Stein, las@ai.mit.edu (semaphore design) * @version $Id: GBS.java,v 1.3 2003/09/23 15:36:21 gus Exp $ * */ public class GBS extends GraphicalSemaphore { /** The current state of the semaphore. * true => busy , false => free */ private boolean busy; /** The graphical display of the semaphore */ private ColorField sqr; //GBS(boolean, String) /** * Constructs a binary semaphore with the intial value passed in. * * @param initVal Initial value for semaphore. (true => in use) * @param label Used to identfy the semaphore in the display. */ public GBS (boolean initVal, String label) { super(label); this.busy = initVal; // now that everything is initialized setup the GUI this.setupGUI(); } /** * Does all of the graphical setup on this level * then calls the superclasses method to finish the setup. * * This method is primarly responsible for setting up the * display Panel. */ protected void setupGUI() { // Graphics setup on this level this.sqr = new ColorField(this.busy, new Dimension(25,25), Color.red, Color.green); this.display = new Panel(); this.display.add(this.sqr); // Graphics setup in super-class super.setupGUI(); } /** * Requests the semaphore. If the semaphore is currently busy, * causes the requesting process to wait() until the semaphore is * release()d. Unlike java.lang.Object.wait(), the requesting * process is not suspended if the semaphore is currently free. * * @see #release * @see java.lang.Object#wait */ synchronized public void request () { while (this.busy) { try {this.wait();} catch (InterruptedException e) {} } this.busy = true; this.showStatus(); } /** * Releases the semaphore. Any objects currently wait()ing on the * semaphore are notify()d (and one of them will be granted the * semaphore). Unlike java.lang.Object.notify(), the semaphore is * also freed so that if there are no wait()ing objects, the next * object to request() the semaphore will receive it. * * @see #request * @see java.lang.Object#notifyAll() */ synchronized public void release () { this.busy = false; this.notifyAll(); this.showStatus(); } /** * Prints out the current state of the semaphore. * Changes the graphical display. */ protected void showStatus() { // tab over an approprate amount for (int i = 0; i