Code example for Lock

Methods: lock, newCondition, unlock

0
package rcm.gaara.concurrency.jcip.chapter14_4; 
 
import java.util.concurrent.locks.Condition; 
import java.util.concurrent.locks.Lock; 
import java.util.concurrent.locks.ReentrantLock; 
 
public class SemaphoreOnLock { 
	private final Lock lock = new ReentrantLock();
	// condition predicate: permitsAvailable (permits>0) 
	private Condition permitesAvailable = lock.newCondition();
	private int permits;
 
	SemaphoreOnLock(int initialPermits) {
		this.lock.lock();
		try { 
			this.permits = initialPermits;
		} finally { 
			this.lock.unlock();
		} 
	} 
 
	/** 
	 * block until permitsAvailable 
	 *  
	 * @throws InterruptedException