001    /*
002    
003      $Id: Mutex.java,v 1.1 2003/03/25 20:33:49 culdesac Exp $
004    
005    */
006    
007    package sharpster.common;
008    
009    /**
010     *
011     */
012    public class Mutex {
013    
014        private boolean locked;
015    
016        /**
017         * Constructs a client communication object
018         */
019        public Mutex() {
020            locked = false;
021        }
022    
023        synchronized public void acquire() {
024            while (locked == true) {
025                try {
026                    this.wait();
027                } catch (Exception e) {
028                    /* ignore */
029                }
030            }
031                
032            locked = true;
033        }
034        
035        synchronized public void release() {
036            locked = false;
037            /* notify any waiting threads on this mutex to wake up */
038            this.notifyAll();
039        }
040    }
041    
042