Java Code Examples for java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock#lock()

The following examples show how to use java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock#lock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InMemoryCacheStatistics.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public long numGets(String cacheName)
{
    ReadLock readLock = getReadLock(cacheName);
    readLock.lock();
    try
    {
        Map<OpType, OperationStats> cacheStats = cacheToStatsMap.get(cacheName);
        if (cacheStats == null)
        {
            throw new NoStatsForCache(cacheName);
        }
        long hits = cacheStats.get(OpType.GET_HIT).getCount();
        long misses = cacheStats.get(OpType.GET_MISS).getCount();
        return hits+misses;
    }
    finally
    {
        readLock.unlock();
    }
}
 
Example 2
Source File: PoolUtils.java    From commons-pool with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int getNumActive() {
    final ReadLock readLock = readWriteLock.readLock();
    readLock.lock();
    try {
        return pool.getNumActive();
    } finally {
        readLock.unlock();
    }
}
 
Example 3
Source File: PoolUtils.java    From commons-pool with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int getNumIdle() {
    final ReadLock readLock = readWriteLock.readLock();
    readLock.lock();
    try {
        return pool.getNumIdle();
    } finally {
        readLock.unlock();
    }
}
 
Example 4
Source File: MapWithLock.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @return
 * @author tanyaowu
 */
public int size() {
	ReadLock readLock = this.readLock();
	readLock.lock();
	try {
		Map<K, V> map = this.getObj();
		return map.size();
	} finally {
		readLock.unlock();
	}
}
 
Example 5
Source File: ADProcessInstanceController.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 5 votes vote down vote up
IAutoCloseable lockForReading()
{
	final ReadLock readLock = readwriteLock.readLock();
	logger.debug("Acquiring read lock for {}: {}", this, readLock);
	readLock.lock();
	logger.debug("Acquired read lock for {}: {}", this, readLock);

	return () -> {
		readLock.unlock();
		logger.debug("Released read lock for {}: {}", this, readLock);
	};
}
 
Example 6
Source File: ObjWithLock.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 操作obj时,带上读锁
 * @param readLockHandler
 */
public void handle(ReadLockHandler<T> readLockHandler) {
	ReadLock readLock = lock.readLock();
	readLock.lock();
	try {
		readLockHandler.handler(obj);
	} catch (Throwable e) {
		log.error(e.getMessage(), e);
	} finally {
		readLock.unlock();
	}
}
 
Example 7
Source File: PoolUtils.java    From commons-pool with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int getNumActive() {
    final ReadLock readLock = readWriteLock.readLock();
    readLock.lock();
    try {
        return keyedPool.getNumActive();
    } finally {
        readLock.unlock();
    }
}
 
Example 8
Source File: Bug3019763.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@ExpectWarning("UL_UNRELEASED_LOCK_EXCEPTION_PATH")
public void doTest2A(OutputStream o) throws IOException {
    ReadLock readLock = rwLock.readLock();
    readLock.lock();

    o.write(0);
    readLock.unlock();

}
 
Example 9
Source File: ASIDocument.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 5 votes vote down vote up
IAutoCloseable lockForReading()
{
	// assume _lock is not null
	final ReadLock readLock = _lock.readLock();
	logger.debug("Acquiring read lock for {}: {}", this, readLock);
	readLock.lock();
	logger.debug("Acquired read lock for {}: {}", this, readLock);

	return () -> {
		readLock.unlock();
		logger.debug("Released read lock for {}: {}", this, readLock);
	};
}
 
Example 10
Source File: FlashPolicyServerStarter.java    From t-io with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {

	while (true) {
		try {
			Thread.sleep(10 * 1000);
		} catch (InterruptedException e1) {
			log.error(e1.toString(), e1);
		}

		SetWithLock<ChannelContext> setWithLock = serverTioConfig.connections;
		Set<ChannelContext> set = null;
		ReadLock readLock = setWithLock.readLock();
		readLock.lock();
		try {
			long now = SystemTimer.currTime;
			set = setWithLock.getObj();
			for (ChannelContext channelContext : set) {
				long interval = (now - channelContext.stat.timeFirstConnected);
				if (interval > 5000) {
					Tio.remove(channelContext, "已经连上来有" + interval + "ms了,该断开啦");
				}
			}
		} catch (java.lang.Throwable e) {
			log.error("", e);
		} finally {
			readLock.unlock();
		}
	}
}
 
Example 11
Source File: PoolUtils.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int getNumIdle() {
    final ReadLock readLock = readWriteLock.readLock();
    readLock.lock();
    try {
        return pool.getNumIdle();
    } finally {
        readLock.unlock();
    }
}
 
Example 12
Source File: Bug3019763.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@ExpectWarning("UL_UNRELEASED_LOCK")
public void doTest2A(boolean b) {
    ReadLock readLock = rwLock.readLock();
    readLock.lock();

    if (b)
        return;
    System.out.println("testing");
    readLock.unlock();

}
 
Example 13
Source File: CachingContentStore.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * This store handles the {@link FileContentStore#SPOOF_PROTOCOL} so that underlying stores do not need
 * to implement anything <a href="https://issues.alfresco.com/jira/browse/ACE-4516">related to spoofing</a>.
 */
@Override
public ContentReader getReader(String contentUrl)
{
    // Handle the spoofed URL
    if (contentUrl.startsWith(FileContentStore.SPOOF_PROTOCOL))
    {
        return new SpoofedTextContentReader(contentUrl);
    }

    // Use pool of locks - which one is determined by a hash of the URL.
    // This will stop the content from being read/cached multiple times from the backing store
    // when it should only be read once - cached versions should be returned after that.
    ReadLock readLock = readWriteLock(contentUrl).readLock();
    readLock.lock();
    try
    {
        if (cache.contains(contentUrl))
        {
            return cache.getReader(contentUrl);
        }
    }
    catch(CacheMissException e)
    {
        // Fall through to cacheAndRead(url);
    }
    finally
    {
        readLock.unlock();
    }
    
    return cacheAndRead(contentUrl);
}
 
Example 14
Source File: PoolUtils.java    From commons-pool with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int getNumActive(final K key) {
    final ReadLock readLock = readWriteLock.readLock();
    readLock.lock();
    try {
        return keyedPool.getNumActive(key);
    } finally {
        readLock.unlock();
    }
}
 
Example 15
Source File: UsingExplicitReadWriteLocks.java    From java-concurrency-patterns with MIT License 5 votes vote down vote up
/**
 * Simplest way to use the read mode
 * 
 * @return
 */
public String showContent() {
	ReadLock readLock = readWriteLock.readLock();
	readLock.lock();
	try {
		System.out.println("Reading state while holding a lock.");
		return myContent;
	} finally {
		readLock.unlock();
	}
}
 
Example 16
Source File: ConcurrentCountingMap.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
/** Gets the value of the counter associated with a given key.
 *
 * @param array a byte array.
 * @param offset the first valid byte in {@code array}.
 * @param length the number of valid elements in {@code array}.
 * @return the current value of the counter associated with the specified key.
 */
public int get(final byte[] array, final int offset, final int length) {
	final long hash = MurmurHash3.hash(array, offset, length);
	final ReadLock readLock = lock[(int)(hash >>> shift)].readLock();
	try {
		readLock.lock();
		return stripe[(int)(hash >>> shift)].get(array, offset, length, hash);
	}
	finally {
		readLock.unlock();
	}
}
 
Example 17
Source File: PoolUtils.java    From commons-pool with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int getNumIdle(final K key) {
    final ReadLock readLock = readWriteLock.readLock();
    readLock.lock();
    try {
        return keyedPool.getNumIdle(key);
    } finally {
        readLock.unlock();
    }
}
 
Example 18
Source File: PoolUtils.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int getNumActive(final K key) {
    final ReadLock readLock = readWriteLock.readLock();
    readLock.lock();
    try {
        return keyedPool.getNumActive(key);
    } finally {
        readLock.unlock();
    }
}
 
Example 19
Source File: SetWithLock.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @return
 * @author tanyaowu
 */
public int size() {
	ReadLock readLock = this.readLock();
	readLock.lock();
	try {
		Set<T> set = this.getObj();
		return set.size();
	} finally {
		readLock.unlock();
	}
}
 
Example 20
Source File: Document.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 5 votes vote down vote up
public IAutoCloseable lockForReading()
{
	// assume _lock is not null
	final ReadLock readLock = _lock.readLock();
	logger.debug("Acquiring read lock for {}: {}", this, readLock);
	readLock.lock();
	logger.debug("Acquired read lock for {}: {}", this, readLock);

	return () -> {
		readLock.unlock();
		logger.debug("Released read lock for {}: {}", this, readLock);
	};
}