public class Semaphore2
{
	private int s;
	private int init;
	
	public Semaphore2(int s)
	{
		assert(s > 0);
		this.s = s;
		init = s;
	} //constructor
	
	public synchronized void p()
	{
		while (s == 0)
		{
			try
			{
				wait();
			} //try
			catch (InterruptedException ie)
			{
			System.out.println("An InterruptedException caught\n"+ie.getMessage());
			ie.printStackTrace();
			} //catch
		} //if
		s--;
	} //p()
	
	public synchronized void v()
	{
		s++;
		notify();
	} //v()
	
	public synchronized void pAll()
	{
		while (s == 0)
		{
			try
			{
				wait();
			} //try
			catch (InterruptedException ie)
			{
			System.out.println("An InterruptedException caught\n"+ie.getMessage());
			ie.printStackTrace();
			} //catch
		} //if
		s=0;	
	} //pAll()

	public synchronized void vAll()
	{
		s = init;
		notify();
	} //vAll()	
} //class Semaphore2