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

The following examples show how to use java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock#unlock() . 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 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 2
Source File: InMemoryCacheStatistics.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 3
Source File: LockUtils.java    From t-io with 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 4
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 5
Source File: MapWithLock.java    From t-io with 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 6
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 7
Source File: MapWithLock.java    From t-io with 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 8
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 9
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 10
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);
	};
}
 
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_EXCEPTION_PATH")
public void doTest2A(OutputStream o) throws IOException {
    ReadLock readLock = rwLock.readLock();
    readLock.lock();

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

}
 
Example 13
Source File: QuickInput.java    From metasfresh-webui-api-legacy with 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 14
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() {
    ReadLock readLock = rwLock.readLock();
    readLock.lock();

    System.out.println("testing");
    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: 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 17
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 18
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 19
Source File: Tio.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * 发消息给指定用户
 * @param tioConfig
 * @param userid
 * @param packet
 * @param isBlock
 * @author tanyaowu
 */
private static Boolean sendToUser(TioConfig tioConfig, String userid, Packet packet, boolean isBlock) {
	SetWithLock<ChannelContext> setWithLock = tioConfig.users.find(tioConfig, userid);
	try {
		if (setWithLock == null) {
			return false;
		}

		ReadLock readLock = setWithLock.readLock();
		readLock.lock();
		try {
			Set<ChannelContext> set = setWithLock.getObj();
			boolean ret = false;
			for (ChannelContext channelContext : set) {
				boolean singleRet = false;
				// 不要用 a = a || b(),容易漏执行后面的函数
				if (isBlock) {
					singleRet = bSend(channelContext, packet);
				} else {
					singleRet = send(channelContext, packet);
				}
				if (singleRet) {
					ret = true;
				}
			}
			return ret;
		} catch (Throwable e) {
			log.error(e.getMessage(), e);
		} finally {
			readLock.unlock();
		}
		return false;
	} finally {
		if (tioConfig.isCluster() && !packet.isFromCluster()) {
			TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();

			if (tioClusterConfig.isCluster4user()) {
				notifyClusterForUser(tioConfig, userid, packet);
			}
		}
	}
}
 
Example 20
Source File: Tio.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * 发消息给指定token
 * @param tioConfig
 * @param token
 * @param packet
 * @param isBlock
 * @author tanyaowu
 */
private static Boolean sendToToken(TioConfig tioConfig, String token, Packet packet, boolean isBlock) {
	SetWithLock<ChannelContext> setWithLock = tioConfig.tokens.find(tioConfig, token);
	try {
		if (setWithLock == null) {
			return false;
		}

		ReadLock readLock = setWithLock.readLock();
		readLock.lock();
		try {
			Set<ChannelContext> set = setWithLock.getObj();
			boolean ret = false;
			for (ChannelContext channelContext : set) {
				boolean singleRet = false;
				// 不要用 a = a || b(),容易漏执行后面的函数
				if (isBlock) {
					singleRet = bSend(channelContext, packet);
				} else {
					singleRet = send(channelContext, packet);
				}
				if (singleRet) {
					ret = true;
				}
			}
			return ret;
		} catch (Throwable e) {
			log.error(e.getMessage(), e);
		} finally {
			readLock.unlock();
		}
		return false;
	} finally {
		if (tioConfig.isCluster() && !packet.isFromCluster()) {
			TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();

			if (tioClusterConfig.isCluster4user()) {
				notifyClusterForToken(tioConfig, token, packet);
			}
		}
	}
}