Code example for Semaphore

Methods: releasetryAcquire

0
 
		try { 
			if (config.isWaitUnlimitOnBorrow()) {
				borrowingSemaphore.acquire();
			} else { 
				boolean acquireSuccess = borrowingSemaphore.tryAcquire(timeout, unit);
				if (!acquireSuccess) {
					// pool entries all busy 
					throw new TimeoutException("borrowEntry timed out.");
				} 
			} 
		} catch (InterruptedException e) {
			throw e;
		} 
		 
		return innerBorrowEntry(createNew);
	} 
 
	 
	@Override 
	public PoolEntry<T> tryBorrowEntry() throws PoolException { 
		return tryBorrowEntry(true); 
	} 
	 
	@Override 
	public PoolEntry<T> tryBorrowEntry(boolean createNew) throws PoolException {
		boolean acquireSuccess = borrowingSemaphore.tryAcquire();
		if (!acquireSuccess) {
			return null; 
		} 
		 
		return innerBorrowEntry(createNew);
	} 
	 
	private PoolEntry<T> innerBorrowEntry(boolean createNew) throws PoolException {
		try { 
			PoolEntry<T> entry = idleEntries.poll();
			if (entry == null && createNew) {
				entry = createIdleEntry();
			} 
			 
			if (entry == null) {
				borrowingSemaphore.release();
			} 
			return entry;
		} catch (Exception e) {
			borrowingSemaphore.release();
			throw new PoolException(e);
		} 
	} 
 
	private PoolEntry<T> createIdleEntry() throws Exception { 
Contextual code suggestions in your IDE  Get Codota for Java