Example usage for java.util.concurrent.locks ReentrantReadWriteLock writeLock

List of usage examples for java.util.concurrent.locks ReentrantReadWriteLock writeLock

Introduction

In this page you can find the example usage for java.util.concurrent.locks ReentrantReadWriteLock writeLock.

Prototype

public ReentrantReadWriteLock.WriteLock writeLock() 

Source Link

Usage

From source file:org.polymap.core.runtime.LockUtils.java

/**
 * Simply *release* read lock and aquire write lock afterwards.
 * <p>//from ww  w .j  av a 2 s . 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:com.aerospike.delivery.db.base.Database.java

public static boolean withWriteLock(ReentrantReadWriteLock lock, Callable<Boolean> action) {
    lock.writeLock().lock();
    try {/*from   ww w  .  j a  va  2 s. c  o  m*/
        return action.call();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        lock.writeLock().unlock();
    }
}

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 ww  .  j  a v  a  2s.co  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:Main.java

public static boolean saveContentToFile(byte[] content, File fileForSave) {
    ReentrantReadWriteLock.WriteLock writeLock = getLock(fileForSave.getAbsolutePath()).writeLock();
    boolean succeed = true;
    FileOutputStream out = null;//from  ww w. ja  va 2  s  .  co  m
    if (writeLock.tryLock()) {
        try {
            out = new FileOutputStream(fileForSave, false);
            out.write(content);
        } catch (Exception var9) {
            //log.d(var9.toString());
            succeed = false;
        } finally {
            if (out != null) {
                closeQuietly(out);
            }

            writeLock.unlock();
        }
    }

    return succeed;
}

From source file:com.espertech.esper.core.StatementRWLockImpl.java

/**
 * Ctor.//from w w w. j  av a 2 s .  co  m
 * @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:com.espertech.esper.multithread.TestMTIsolation.java

private void tryIsolated(int numThreads, int numLoops) throws Exception {
    Configuration config = SupportConfigFactory.getConfiguration();
    config.getEngineDefaults().getViewResources().setShareViews(false);
    config.addEventType("SupportBean", SupportBean.class);
    EPServiceProvider engine = EPServiceProviderManager.getDefaultProvider(config);
    engine.initialize();// w  w  w.  j ava2  s  . co  m

    // execute
    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    Future future[] = new Future[numThreads];
    ReentrantReadWriteLock sharedStartLock = new ReentrantReadWriteLock();
    sharedStartLock.writeLock().lock();
    for (int i = 0; i < numThreads; i++) {
        future[i] = threadPool.submit(new IsolateUnisolateCallable(i, engine, numLoops));
    }
    Thread.sleep(100);
    sharedStartLock.writeLock().unlock();

    threadPool.shutdown();
    threadPool.awaitTermination(10, TimeUnit.SECONDS);

    for (int i = 0; i < numThreads; i++) {
        assertTrue((Boolean) future[i].get());
    }
}

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  ww . java  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:org.apache.hadoop.yarn.server.resourcemanager.PendingEventRetrievalBatch.java

/**
 * @param rmContext/*from w  w w.j av  a  2s  .c o  m*/
 * @param period
 */
public PendingEventRetrievalBatch(RMContext rmContext, Configuration conf) {
    super(rmContext, conf);
    this.firstRetrieval = true;
    this.period = conf.getInt(YarnConfiguration.HOPS_PENDING_EVENTS_RETRIEVAL_PERIOD,
            YarnConfiguration.DEFAULT_HOPS_PENDING_EVENTS_RETRIEVAL_PERIOD);
    LOG.debug("PendingEventRetrieval period=" + period);
    this.pendingEvents = new HashMap<String, ConcurrentSkipListSet<PendingEvent>>();
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    this.writeLock = lock.writeLock();
}

From source file:org.bremersee.objectlock.ObjectReadWriteLockImpl.java

@Override
public void unlockWriting(Object obj) {

    if (obj == null) {
        return;//ww  w.  java  2s . c om
    }
    synchronized (locks) {
        ReentrantReadWriteLock l = locks.get(obj);
        if (l != null) {
            l.writeLock().unlock();
            if (l.getQueueLength() == 0) {
                l = locks.remove(obj);
                returnLock(l);
            }
        }
    }
}