I am bit stuck here, I have to have edit this code so that a train must wait in front of the tunnel lock for a train to come in the opposite direction to exit before it can enter the tunnel, i have to use java built in synchronisation condition. Before the try statement I have tried looking at while statements, if statements and still cant get it to give me the following output:

Train 1 arrives
Train 1 ascends
Train 1 Exits Tunnel
Train 2 Arrives
train 2 descends
train 2 exits tunnel
train 3 and so on

Any help would be greatful

public synchronized void useTunnelLock(Train train)

System.out.println(train + " " + train.getDirection());
System.out.println(train + " exiting Tunnel");
while(train.getDirection() == ASCENDING())
// occupy tunnel lock for 5s
catch (InterruptedException ex)

// swap direction of tunnel lock
direction = (direction == ASCENDING)? DESCENDING : ASCENDING;