home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / util / cubbyhole.java next >
Encoding:
Java Source  |  1996-08-14  |  769 b   |  46 lines

  1. /**
  2.  * CubbyHole based on the CubbyHole from the progguide.
  3.  *
  4.  */
  5.  
  6. package como.util;
  7.  
  8.  
  9. /**
  10.  * Use this to communicate between processes.
  11.  * Put in an Object and read it from the other side!
  12.  *
  13.  * As long as nobody put()'s an Object into it, the
  14.  * get() will block!
  15.  */
  16.  
  17. public class CubbyHole {
  18.     private Object obj;
  19.     private boolean available = false;
  20.  
  21.     /**
  22.      * gets the Object from the CubbyHole.
  23.      * @return the Object.
  24.      */
  25.     public synchronized Object get() {
  26.         while( available == false ) {
  27.             try {
  28.                 wait();
  29.             } catch( InterruptedException e ) {
  30.             }
  31.         }
  32.         available = false;
  33.         return obj;
  34.     }
  35.  
  36.     /**
  37.      * put an Object in the CubbyHole.
  38.      * @param o the Object.
  39.      */
  40.     public synchronized void put( Object o ) {
  41.         obj = o;
  42.         available = true;
  43.         notify();
  44.     }
  45. }
  46.