Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.Semaphore;
- public class vehicle implements Runnable
- {
- static int numCars=0;
- private int carNum;
- private boolean north;
- private boolean crossed= false;
- static Semaphore mutex;
- public vehicle(boolean isNorth)
- {
- north=isNorth;
- numCars=numCars+1;
- carNum=numCars;
- mutex=new Semaphore(1);
- }
- public void run()
- {
- System.out.println("car number: "+carNum+" has arrived at bridge.");
- crossBridge(); )
- }
- public void setCrossed()
- {
- crossed=true;
- }
- public int getNumber()
- {
- return carNum;
- }
- public Boolean crossBridge()
- {
- try
- {
- mutex.acquire();
- }
- catch(InterruptedException e)
- {System.out.println("mutex failed to acquire.");}
- setCrossed();
- mutex.release();
- System.out.println("car number "+carNum+" has crossed the bridge.");
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement