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

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

Introduction

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

Prototype

public ReentrantReadWriteLock() 

Source Link

Document

Creates a new ReentrantReadWriteLock with default (nonfair) ordering properties.

Usage

From source file:org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacities.java

public QueueCapacities(boolean isRoot) {
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    readLock = lock.readLock();//from  w  w  w  . ja  v a  2  s .com
    writeLock = lock.writeLock();

    capacitiesMap = new HashMap<String, Capacities>();
    this.isRoot = isRoot;
}

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();//from  w  w w  .j  a  va  2s . 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.alfresco.util.registry.NamedObjectRegistry.java

/**
 * Default constructor.  The {@link #setStorageType(Class)} method must be called.
 *//*from   w ww.ja v  a 2 s .  c  o  m*/
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);
}

From source file:org.alfresco.util.PathMapper.java

/**
 * Default constructor/*from   w  ww  .ja  v  a 2s  . co  m*/
 */
public PathMapper() {
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    readLock = lock.readLock();
    writeLock = lock.writeLock();

    pathMaps = new HashMap<String, Set<String>>(37);
    derivedPathMaps = new HashMap<String, Set<String>>(127);
    derivedPathMapsPartial = new HashMap<String, Set<String>>(127);
}

From source file:com.espertech.esper.filter.FilterParamIndexCompare.java

public FilterParamIndexCompare(FilterSpecLookupable lookupable, FilterOperator filterOperator) {
    super(filterOperator, lookupable);

    constantsMap = new TreeMap<Object, EventEvaluator>();
    constantsMapRWLock = new ReentrantReadWriteLock();

    if ((filterOperator != FilterOperator.GREATER) && (filterOperator != FilterOperator.GREATER_OR_EQUAL)
            && (filterOperator != FilterOperator.LESS) && (filterOperator != FilterOperator.LESS_OR_EQUAL)) {
        throw new IllegalArgumentException("Invalid filter operator for index of " + filterOperator);
    }/*from www .j  a  v  a 2  s . c  o  m*/
}

From source file:com.alibaba.zonda.logger.server.writer.OutputStreamManager.java

private OutputStreamManager(String baseDir) {
    this.lock = new ReentrantReadWriteLock();
    this.baseDir = baseDir;
    this.osMap = new ConcurrentHashMap<String, LogOutputStream>(5);
}

From source file:org.rhq.enterprise.server.util.concurrent.InventoryReportSerializer.java

public void lock(String agentName) {
    String msg = "tid=" + Thread.currentThread().getId() + "; agent=" + agentName;
    boolean debug = this.log.isDebugEnabled();

    ReentrantReadWriteLock lock = null;
    logDebug(debug, msg, ": about to synchronize");
    synchronized (this) {
        logDebug(debug, msg, ": synchronized");
        lock = InventoryReportSerializer.locks.get(agentName);
        if (lock == null) {
            logDebug(debug, msg, ": creating new lock");
            lock = new ReentrantReadWriteLock();
            InventoryReportSerializer.locks.put(agentName, lock);
        }// w w w  . ja  v a  2 s . c o  m
    }

    logDebug(debug, msg, ": acquiring write lock");
    long start = System.currentTimeMillis();
    lock.writeLock().lock();
    long end = System.currentTimeMillis();
    long duration = end - start;
    InventoryReportSerializer.lockTimes.put(agentName, Long.valueOf(end));
    if (duration < 5000L) {
        logDebug(debug, msg, ": acquired write lock in millis=" + duration);
    } else {
        this.log.info(msg + ": acquired write lock in millis=" + duration);
    }

    return;
}

From source file:org.apache.servicemix.jbi.cluster.engine.ClusterEndpointLoadTest.java

public void testLoadInOnly() throws Exception {
    createRoute(Transacted.Jms, true, false, true);

    final int nbThreads = 10;
    final int nbExchanges = 10;
    final ReadWriteLock lock = new ReentrantReadWriteLock();
    final CountDownLatch latch = new CountDownLatch(nbThreads);
    final AtomicInteger id = new AtomicInteger();
    lock.writeLock().lock();//from   ww  w.  ja  v a2 s  .  c  om
    for (int i = 0; i < nbThreads; i++) {
        new Thread() {
            public void run() {
                Channel client = null;
                try {
                    client = nmr1.createChannel();
                    lock.readLock().lock();
                    for (int i = 0; i < nbExchanges; i++) {
                        Exchange exchange = client.createExchange(Pattern.InOnly);
                        exchange.getIn()
                                .setBody(new StringSource("<hello id='" + id.getAndIncrement() + "'/>"));
                        exchange.setTarget(nmr1.getEndpointRegistry()
                                .lookup(ServiceHelper.createMap(Endpoint.NAME, PROXY_ENDPOINT_NAME)));
                        client.sendSync(exchange);
                        assertEquals(Status.Done, exchange.getStatus());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.readLock().unlock();
                    latch.countDown();
                    if (client != null) {
                        client.close();
                    }
                }
            }
        }.start();
    }

    long t0, t1;

    t0 = System.currentTimeMillis();
    lock.writeLock().unlock();

    latch.await();

    receiver.assertExchangesReceived(nbThreads * nbExchanges, TIMEOUT);

    t1 = System.currentTimeMillis();

    System.err.println("Elapsed time: " + (t1 - t0) + " ms");
    System.err.println("Throuput: " + (nbThreads * nbExchanges * 1000 / (t1 - t0)) + " messages/sec");
}

From source file:org.rhq.enterprise.server.util.concurrent.AvailabilityReportSerializer.java

public void lock(String agentName) {
    String msg = "tid=" + Thread.currentThread().getId() + "; agent=" + agentName;
    boolean debug = this.log.isDebugEnabled();

    ReentrantReadWriteLock lock = null;
    logDebug(debug, msg, ": about to synchronize");
    synchronized (this) {
        logDebug(debug, msg, ": synchronized");
        lock = AvailabilityReportSerializer.locks.get(agentName);
        if (lock == null) {
            logDebug(debug, msg, ": creating new lock");
            lock = new ReentrantReadWriteLock();
            AvailabilityReportSerializer.locks.put(agentName, lock);
        }//from   ww w.j a  v a2  s.c  o m
    }

    logDebug(debug, msg, ": acquiring write lock");
    long start = System.currentTimeMillis();
    lock.writeLock().lock();
    long end = System.currentTimeMillis();
    long duration = end - start;
    AvailabilityReportSerializer.lockTimes.put(agentName, Long.valueOf(end));
    if (duration < 5000L) {
        logDebug(debug, msg, ": acquired write lock in millis=" + duration);
    } else {
        this.log.info(msg + ": acquired write lock in millis=" + duration);
    }

    return;
}

From source file:fm.last.moji.impl.MojiFileImpl.java

MojiFileImpl(String key, String domain, String storageClass, TrackerFactory trackerFactory,
        HttpConnectionFactory httpFactory) {
    this.key = key;
    this.domain = domain;
    this.storageClass = storageClass;
    this.trackerFactory = trackerFactory;
    this.httpFactory = httpFactory;
    executor = new Executor(trackerFactory);
    lock = new ReentrantReadWriteLock();
}