import java.util.concurrent.Semaphore;

public class SemaphoreProblem extends MutexProblem {

	
	//shared Semaphore variable
	Semaphore lock = new Semaphore(5); //available permits
	
	@Override
	AbstractProcess createProcess(int type) {
		return new AbstractProcess(type) {

			@Override
			protected boolean entrySection() {
				try {
					lock.acquire();
				} catch (InterruptedException e) {
					e.printStackTrace();
					//failed to get the lock
					return false;
				}
				return true;
			}

			@Override
			protected void exitSection() {
				lock.release();
			}
			
		};
	}

	@Override
	public String toString() {
		return String.format("Permits available: %d", lock.availablePermits());
	}

}
