List of usage examples for java.util.concurrent.locks ReentrantReadWriteLock readLock
public ReentrantReadWriteLock.ReadLock readLock()
From source file:org.polymap.core.runtime.LockUtils.java
/** * Simply *release* read lock and aquire write lock afterwards. * <p>//from w w w . j ava 2s .c om * This is not a real upgrade as the real locl is <b>released</b> * before the write lock is aquired! * * @param rwLock */ public static void upgrade(ReentrantReadWriteLock rwLock) { rwLock.readLock().unlock(); rwLock.writeLock().lock(); }
From source file:org.polymap.core.runtime.LockUtils.java
/** * Executes the given callable inside read lock of the given lock. The call() * method may aquire the write lock during execution. Both locks are released * after this method returns./*from w w w.j av a 2s . c o m*/ * * @param <V> * @param rwLock * @param task * @return The result of the given {@link #task}. * @throws Exception The exception from the {@link #task}. */ public static <V> V withReadLock(ReentrantReadWriteLock rwLock, Callable<V> task) throws Exception { try { rwLock.readLock().lock(); return task.call(); } finally { if (rwLock.writeLock().isHeldByCurrentThread()) { rwLock.readLock().lock(); rwLock.writeLock().unlock(); } rwLock.readLock().unlock(); } }
From source file:com.aerospike.delivery.db.base.Database.java
public static boolean withReadLock(ReentrantReadWriteLock lock, Callable<Boolean> action) { lock.readLock().lock(); try {/*from w ww. j a v a 2 s .c om*/ return action.call(); } catch (Exception e) { e.printStackTrace(); return false; } finally { lock.readLock().unlock(); } }
From source file:Main.java
public static byte[] readContentBytesFromFile(File fileForRead) { if (fileForRead == null) { return null; } else if (fileForRead.exists() && fileForRead.isFile()) { ReentrantReadWriteLock.ReadLock readLock = getLock(fileForRead.getAbsolutePath()).readLock(); readLock.lock();//w w w . j av a2s.c om Object data = null; BufferedInputStream input = null; try { byte[] data1 = new byte[(int) fileForRead.length()]; int e = 0; input = new BufferedInputStream(new FileInputStream(fileForRead), 8192); while (e < data1.length) { int bytesRemaining = data1.length - e; int bytesRead = input.read(data1, e, bytesRemaining); if (bytesRead > 0) { e += bytesRead; } } byte[] bytesRemaining1 = data1; return bytesRemaining1; } catch (IOException var10) { } finally { closeQuietly(input); readLock.unlock(); } return null; } else { return null; } }
From source file:org.apache.hadoop.yarn.server.resourcemanager.placement.PlacementManager.java
public PlacementManager() { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); readLock = lock.readLock(); writeLock = lock.writeLock(); }
From source file:com.amazonaws.internal.FIFOCache.java
/** * @param maxSize/*from w w w . j av a 2 s . co m*/ * the maximum number of entries of the cache */ public FIFOCache(final int maxSize) { if (maxSize < 1) { throw new IllegalArgumentException("maxSize " + maxSize + " must be at least 1"); } map = new BoundedLinkedHashMap<String, T>(maxSize); ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); rlock = lock.readLock(); wlock = lock.writeLock(); }
From source file:com.espertech.esper.core.StatementRWLockImpl.java
/** * Ctor./*from www. java2 s. com*/ * @param name of lock * @param isFair true if a fair lock, false if not */ public StatementRWLockImpl(String name, boolean isFair) { this.name = name; ReentrantReadWriteLock lock = new ReentrantReadWriteLock(isFair); writeLock = lock.writeLock(); readLock = lock.readLock(); }
From source file:org.jactr.core.module.declarative.search.map.SortedValueMap.java
@Override public Collection<I> lessThan(V value) { if (value == null) throw new NullPointerException("null values are not permitted as keys"); Set<I> rtn = instantiateReturnSet(); ReentrantReadWriteLock lock = getLock(); try {//from w ww .ja va 2 s. co m lock.readLock().lock(); for (V tmpValue : _sortedValues.headSet(value)) rtn.addAll(get(tmpValue)); // TreeMap<V, Collection<I>> coreMap = (TreeMap<V, Collection<I>>) getCoreMap(); // // Map<V, Collection<I>> head = coreMap.headMap(value); // for (Collection<I> values : head.values()) // if (values != null) rtn.addAll(values); return rtn; } finally { lock.readLock().unlock(); } }
From source file:org.jactr.core.module.declarative.search.map.SortedValueMap.java
@Override public Collection<I> greaterThan(V value) { if (value == null) throw new NullPointerException("null values are not permitted as keys"); Set<I> rtn = instantiateReturnSet(); ReentrantReadWriteLock lock = getLock(); try {//from w w w .j a v a 2 s . com lock.readLock().lock(); for (V tmpValue : _sortedValues.tailSet(value)) if (!tmpValue.equals(value)) rtn.addAll(get(tmpValue)); // TreeMap<V, Collection<I>> coreMap = (TreeMap<V, Collection<I>>) getCoreMap(); // // Map<V, Collection<I>> tail = coreMap.tailMap(value); // for (V tmpValue : tail.keySet()) // { // if (!tmpValue.equals(value)) rtn.addAll(get(tmpValue)); // } return rtn; } finally { lock.readLock().unlock(); } }
From source file:org.alfresco.util.registry.NamedObjectRegistry.java
/** * Default constructor. The {@link #setStorageType(Class)} method must be called. *//*from w w w .j av a 2 s . com*/ public NamedObjectRegistry() { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); readLock = lock.readLock(); writeLock = lock.writeLock(); this.namePattern = null; // Deliberately null this.storageType = null; // Deliberately null this.objects = new HashMap<String, T>(13); }