KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > lock > SemaphoreLock


1 package org.jboss.cache.lock;
2
3 import java.util.concurrent.Semaphore JavaDoc;
4 import java.util.concurrent.TimeUnit JavaDoc;
5 import java.util.concurrent.locks.Condition JavaDoc;
6 import java.util.concurrent.locks.Lock JavaDoc;
7
8 /**
9  * Implements most of the methods of Lock using the {@link Semaphore} implementation.
10  */

11 public class SemaphoreLock extends Semaphore JavaDoc implements Lock JavaDoc
12 {
13
14    /**
15     * It's unclear why this class would be serialized.
16     */

17    private static final long serialVersionUID = -15293253129957476L;
18
19    public SemaphoreLock(int permits)
20    {
21       super(permits);
22    }
23
24    public void lock()
25    {
26       try
27       {
28          acquire();
29       }
30       catch (InterruptedException JavaDoc e)
31       {
32          lock(); // recursive, I know ..
33
}
34    }
35
36    public void lockInterruptibly() throws InterruptedException JavaDoc
37    {
38       acquire();
39    }
40
41    public Condition JavaDoc newCondition()
42    {
43       throw new UnsupportedOperationException JavaDoc();
44    }
45
46    public boolean tryLock()
47    {
48       return tryAcquire();
49    }
50
51    public boolean tryLock(long arg0, TimeUnit JavaDoc arg1) throws InterruptedException JavaDoc
52    {
53       return tryAcquire(arg0, arg1);
54    }
55
56    public void unlock()
57    {
58       release();
59    }
60
61 }
62
Popular Tags