java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock Java Examples

The following examples show how to use java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock. 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
@Override
public long count(String cacheName, OpType opType)
{
    ReadLock readLock = getReadLock(cacheName);
    readLock.lock();
    try
    {
        Map<OpType, OperationStats> cacheStats = cacheToStatsMap.get(cacheName);
        if (cacheStats == null)
        {
            throw new NoStatsForCache(cacheName);
        }
        OperationStats opStats = cacheStats.get(opType);
        return opStats.getCount();
    }
    finally
    {
        readLock.unlock();
    }
}
 
Example #2
@Override
public double meanTime(String cacheName, OpType opType)
{
    ReadLock readLock = getReadLock(cacheName);
    readLock.lock();
    try
    {
        Map<OpType, OperationStats> cacheStats = cacheToStatsMap.get(cacheName);
        if (cacheStats == null)
        {
            throw new NoStatsForCache(cacheName);
        }
        OperationStats opStats = cacheStats.get(opType);
        return opStats.meanTime();
    }
    finally
    {
        readLock.unlock();
    }
}
 
Example #3
@Override
public double hitMissRatio(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 (double)hits / (hits+misses);
    }
    finally
    {
        readLock.unlock();
    }
}
 
Example #4
@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 #5
@Override
public Map<OpType, OperationStats> allStats(String cacheName)
{
    ReadLock readLock = getReadLock(cacheName);
    readLock.lock();
    try
    {
        Map<OpType, OperationStats> cacheStats = cacheToStatsMap.get(cacheName);
        if (cacheStats == null)
        {
            throw new NoStatsForCache(cacheName);
        }
        return new HashMap<>(cacheStats);
    }
    finally
    {
        readLock.unlock();
    }
}
 
Example #6
Source Project: Tomcat8-Source-Read   Author: chenmudu   File: PoolUtils.java    License: 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 #7
Source Project: Tomcat8-Source-Read   Author: chenmudu   File: PoolUtils.java    License: MIT License 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 #8
Source Project: Tomcat8-Source-Read   Author: chenmudu   File: PoolUtils.java    License: MIT License 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 #9
Source Project: Tomcat8-Source-Read   Author: chenmudu   File: PoolUtils.java    License: 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 #10
Source Project: Tomcat8-Source-Read   Author: chenmudu   File: PoolUtils.java    License: MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int getNumIdle() {
    final ReadLock readLock = readWriteLock.readLock();
    readLock.lock();
    try {
        return keyedPool.getNumIdle();
    } finally {
        readLock.unlock();
    }
}
 
Example #11
Source Project: Tomcat8-Source-Read   Author: chenmudu   File: PoolUtils.java    License: MIT License 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 #12
Source Project: java-concurrency-patterns   Author: LeonardoZ   File: UsingExplicitReadWriteLocks.java    License: 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 #13
Source Project: alfresco-repository   Author: Alfresco   File: CachingContentStore.java    License: 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 Project: t-io   Author: tywo45   File: ListWithLock.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @return
 * @author tanyaowu
 */
public int size() {
	ReadLock readLock = this.readLock();
	readLock.lock();
	try {
		List<T> list = this.getObj();
		return list.size();
	} finally {
		readLock.unlock();
	}
}
 
Example #15
Source Project: t-io   Author: tywo45   File: ObjWithLock.java    License: 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 #16
Source Project: t-io   Author: tywo45   File: MapWithLock.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param key
 * @return
 * @author tanyaowu
 */
public V get(K key) {
	ReadLock readLock = this.readLock();
	readLock.lock();
	try {
		Map<K, V> map = this.getObj();
		return map.get(key);
	} catch (Throwable e) {
		log.error(e.getMessage(), e);
	} finally {
		readLock.unlock();
	}
	return null;
}
 
Example #17
Source Project: t-io   Author: tywo45   File: MapWithLock.java    License: 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 #18
Source Project: t-io   Author: tywo45   File: MapWithLock.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @return 如果没值,则返回null,否则返回一个新map
 * @author tanyaowu
 */
public Map<K, V> copy() {
	ReadLock readLock = readLock();
	readLock.lock();
	try {
		if (this.getObj().size() > 0) {
			return new HashMap<>(getObj());
		}
		return null;
	} finally {
		readLock.unlock();
	}
}
 
Example #19
Source Project: t-io   Author: tywo45   File: SetWithLock.java    License: 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 Project: t-io   Author: tywo45   File: LockUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
	 * 运行write或者等待读锁<br>
	 * 1、能拿到写锁的线程会执行readWriteLockHandler.write()<br>
	 * 2、没拿到写锁的线程,会等待获取读锁,注:获取到读锁的线程,什么也不会执行<br>
	 * 3、当一段代码只允许被一个线程执行时,才用本函数,不要理解成同步等待了<br>
	 * <br>
	 * <strong>注意:对于一些需要判断null等其它条件才执行的操作,在write()方法中建议再检查一次,这个跟double check的原理是一样的</strong><br>
	 * @param key
	 * @param myLock 获取ReentrantReadWriteLock的锁,可以为null
	 * @param readWriteLockHandler 小心:该对象的write()方法并不一定会被执行
	 * @param readWaitTimeInSecond 没拿到写锁的线程,等读锁的时间,单位:秒
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	public static void runWriteOrWaitRead(String key, Object myLock, ReadWriteLockHandler readWriteLockHandler, Long readWaitTimeInSecond) throws Exception {
		ReentrantReadWriteLock rwLock = getReentrantReadWriteLock(key, myLock);
//		ReadWriteRet ret = new ReadWriteRet();
		WriteLock writeLock = rwLock.writeLock();
		boolean tryWrite = writeLock.tryLock();
		if (tryWrite) {
			try {
				readWriteLockHandler.write();
//				ret.writeRet = writeRet;
			} finally {
//				ret.isWriteRunned = true;
				writeLock.unlock();
			}
		} else {
			ReadLock readLock = rwLock.readLock();
			boolean tryRead = false;
			try {
				tryRead = readLock.tryLock(readWaitTimeInSecond, TimeUnit.SECONDS);
				if (tryRead) {
//					try {
//						readWriteLockHandler.read();
//						ret.readRet = readRet;
//					} finally {
//						ret.isReadRunned = true;
						readLock.unlock();
//					}
				}
			} catch (InterruptedException e) {
				log.error(e.toString(), e);
			}
		}
//		return ret;
	}
 
Example #21
Source Project: t-io   Author: tywo45   File: FlashPolicyServerStarter.java    License: 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 #22
Source Project: BUbiNG   Author: LAW-Unimi   File: ConcurrentCountingMap.java    License: 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 #23
Source Project: metasfresh-webui-api-legacy   Author: metasfresh   File: Document.java    License: 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);
	};
}
 
Example #24
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 #25
Source Project: metasfresh-webui-api-legacy   Author: metasfresh   File: ASIDocument.java    License: 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 #26
Source Project: metasfresh-webui-api-legacy   Author: metasfresh   File: QuickInput.java    License: GNU General Public License v3.0 5 votes vote down vote up
public 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 #27
Source Project: spotbugs   Author: spotbugs   File: Bug3019763.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
@NoWarning("UL_UNRELEASED_LOCK")
public void doTestA() {
    ReadLock readLock = rwLock.readLock();
    readLock.lock();

    System.out.println("testing");

}
 
Example #28
Source Project: spotbugs   Author: spotbugs   File: Bug3019763.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
@ExpectWarning("UL_UNRELEASED_LOCK_EXCEPTION_PATH")
public void doTest2A() {
    ReadLock readLock = rwLock.readLock();
    readLock.lock();

    System.out.println("testing");
    readLock.unlock();

}
 
Example #29
Source Project: spotbugs   Author: spotbugs   File: Bug3019763.java    License: 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 #30
Source Project: spotbugs   Author: spotbugs   File: Bug3019763.java    License: 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();

}