Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jacomma/util/Semaphore.java


1   /*
2    * $Source: /home/data/cvsroot/src/jacomma/util/Semaphore.java,v $
3    * $Revision: 1.4 $
4    * $Date: 2000/10/28 20:09:08 $
5    *
6    * This file is part of the jacomma framework
7    * Copyright (c) 2000   Dimitrios Vyzovitis
8    *      mailto:dviz@egnatia.ee.auth.gr
9    *      
10   *
11   *      
12   *      
13   *      
14   *
15   *  This library is free software; you can redistribute it and/or modify
16   *  it under the terms of the GNU Library General Public License as published by
17   *  the Free Software Foundation; either version 2 of the License, or
18   *  (at your option) any later version.
19   *
20   *  This library is distributed in the hope that it will be useful,
21   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   *  GNU Library General Public License for more details.
24   *
25   *  You should have received a copy of the GNU Library General Public License
26   *  along with this library; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, 
28   *  Boston, MA  02111-1307  USA
29   */
30  
31  package jacomma.util;
32  
33  /**
34   * Simple Semaphore variable (well, not pthread_cond, since this is just synchronization)
35   **/
36  public class Semaphore {
37    
38    private int count_;
39  
40    public Semaphore() {}
41    
42    public synchronized void post() {
43      ++count_;
44      notifyAll();
45    }
46    
47    public synchronized int get() { return count_; }
48    
49    public synchronized void sem_wait() throws InterruptedException {
50      while( count_ == 0 ) wait();
51      --count_;
52    }
53    
54    public synchronized boolean sem_wait( long t ) throws InterruptedException {
55      if ( count_ == 0 ) wait( t );
56      return (count_ > 0) ? (count_-- > 0) : false;
57    }
58  
59  }