KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.cache.lock;
8
9 import EDU.oswego.cs.dl.util.concurrent.Sync;
10 import EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock;
11
12 /**
13  * @author Bela Ban
14  * @version $Id: BelasLockStrategy.java,v 1.1.2.2 2005/04/06 21:06:45 starksm Exp $
15  */

16 public class BelasLockStrategy implements LockStrategy {
17    ReentrantWriterPreferenceReadWriteLock lock=new ReentrantWriterPreferenceReadWriteLock();
18
19    public BelasLockStrategy() {
20    }
21
22    public Sync readLock() {
23       return lock.readLock();
24    }
25
26    public Sync writeLock() {
27       return lock.writeLock();
28    }
29
30    public Sync upgradeLockAttempt(long msecs) throws UpgradeException {
31       boolean acquired;
32       Sync writeLock=lock.writeLock();
33
34       try {
35          acquired=writeLock.attempt(msecs);
36          if(acquired) {
37             lock.readLock().release();
38             return writeLock;
39          }
40          else {
41             return null;
42          }
43       }
44       catch(InterruptedException JavaDoc iex) {
45          throw new UpgradeException("failed upgrading lock", iex);
46       }
47    }
48 }
49
Popular Tags