protected static boolean isEligible(String message) { try { lock.readLock().lock(); if (hashSet.contains(message)) return false; } finally { lock.readLock().unlock(); } try { lock.writeLock().lock(); if (buffer.size() >= 100) { String rem = buffer.remove(); hashSet.remove(rem); } buffer.add(message); hashSet.add(message); return true; } finally { lock.writeLock().unlock(); } }
/** * Creates a new instance of {@link MountTable}. * * @param ufsManager the UFS manager * @param rootMountInfo root mount info */ public MountTable(UfsManager ufsManager, MountInfo rootMountInfo) { mState = new State(rootMountInfo); ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); mReadLock = lock.readLock(); mWriteLock = lock.writeLock(); mUfsManager = ufsManager; }
/** * Returns the Input/Output metadata for this step. By default, each step produces and accepts optional input. */ public StepIOMetaInterface getStepIOMeta( boolean createIfAbsent ) { StepIOMetaInterface ioMeta = null; lock.readLock().lock(); try { if ( ( ioMetaVar == null ) && ( createIfAbsent ) ) { ioMeta = new StepIOMeta( true, true, true, false, false, false ); lock.readLock().unlock(); lock.writeLock().lock(); try { ioMetaVar = ioMeta; lock.readLock().lock(); // downgrade to read lock before releasing write lock } finally { lock.writeLock().unlock(); } } else { ioMeta = ioMetaVar; } return ioMeta; } finally { lock.readLock().unlock(); } }
public DefaultEventBus() { this.registry = new LinkedHashMap<Object, Subscription>(); //not thread safe, so we need locks: ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); this.registryReadLock = rwl.readLock(); this.registryWriteLock = rwl.writeLock(); this.eventListenerResolver = new AnnotationEventListenerResolver(); }
void subscribe() { try { subscriberLock.readLock().lock(); if (subscriber == null) { subscriberLock.readLock().unlock(); subscriberLock.writeLock().lock(); try { try { if (subscriber == null) { waitForSubscription(1, TimeUnit.SECONDS); } } finally { subscriberLock.readLock().lock(); } } finally { subscriberLock.writeLock().unlock(); } } } finally { subscriberLock.readLock().unlock(); } }
/** * @param maxSize * 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(); }