import java.util.concurrent.*;
import java.util.concurrent.locks.*;


public class Schlucht
{

	Lock mutex = new ReentrantLock();
	Condition eastWestAllowed = mutex.newCondition();
    Condition westEastAllowed = mutex.newCondition();
    int passingQueueLength = 0;
    int eastQueueLength = 0;
    int westQueueLengt = 0;


	public void eastWest()
	{
		mutex.lock();
		try {
			while ( eastQueueLength > 0 && passingQueueLength > 0 )
			{
				eastWestAllowed.await();
			}
			passingQueueLength++;
			mutex.unlock();

			pass();

			passingQueueLength--;

			mutex.lock();

			if(eastQueueLengt > 0) {
				if(passingQueueLength == 0) westEastAllowed.signal();
				}
			}
			else {
				eastWestAllowed.signal();
			}

		} finally {
			mutex.unlock();
		}
	}

	public void westEast()
	{
		// analog
	}

	public void pass()
	{
		System.out.println("Passing...");
	}
}