Here is the output, most of the time, :
Commuter 0 has entered the boulevard. He entered lane : 0 at time: 1
Commuter 0 has entered the bridge at time : 1
Commuter 0 will pay cash.
Commuter 0 is entering the toll booth line. Time: 1
Commuter 0 Entered the toll booth. Value is: 0
Commuter 0 Entered the waiting line. Value is: -1
Commuter 0 Will wait now.
Commuter 2 has entered the boulevard. He entered lane : 2 at time: 1
There are double-parked cars in Commuter 2 lane. Time: 1
Commuter 2 is yielding until lane is clear.Time:2
Commuter 4 has entered the boulevard. He entered lane : 1 at time: 2
Commuter 4 has entered the bridge at time : 2
Commuter 4 will pay cash.
Commuter 4 is on a high speed chase. He will speed right through the cash booth. Time: 2
Commuter 4 Entered the toll booth. Value is: -1
Commuter 4 Exited the line. Value: -1
Commuter 4 notifiying the next waiter. Value: 0
Commuter 4 has been caught and placed in jail. Time: 2
The value on the toll is 0
Commuter 6 has entered the boulevard. He entered lane : 2 at time: 2
There are double-parked cars in Commuter 6 lane. Time: 2
Commuter 6 is yielding until lane is clear.Time:2
Road Rage from Commuter 2. Speeding to get to work on time. Time: 2
I must stop since I'm nearing a red light, thinks Commuter 2.
Commuter 2 has entered the bridge at time : 2
Commuter 2 has an EZPass.
Commuter 2 has left the bridge and will head to work.
Commuter 2 has arrived at work. Time: 4
Commuter 1 has entered the boulevard. He entered lane : 2 at time: 4
Road Rage from Commuter 6. Speeding to get to work on time. Time: 4
I must stop since I'm nearing a red light, thinks Commuter 6.
Commuter 6 has entered the bridge at time : 4
Commuter 6 will pay cash.
Commuter 6 is entering the toll booth line. Time: 4
Commuter 6 Entered the toll booth. Value is: 0
Commuter 6 Entered the waiting line. Value is: -1
Commuter 6 Will wait now.
Commuter 8 has entered the boulevard. He entered lane : 0 at time: 4
Commuter 8 has entered the bridge at time : 4
Commuter 8 has an EZPass.
Commuter 8 has left the bridge and will head to work.
Commuter 8 has arrived at work. Time: 5
There are double-parked cars in Commuter 1 lane. Time: 4
Commuter 1 is yielding until lane is clear.Time:5
Road Rage from Commuter 1. Speeding to get to work on time. Time: 5
I must stop since I'm nearing a red light, thinks Commuter 1.
Commuter 1 has entered the bridge at time : 5
Commuter 1 has an EZPass.
Commuter 1 has left the bridge and will head to work.
Commuter 1 has arrived at work. Time: 5
Commuter 3 has entered the boulevard. He entered lane : 0 at time: 5
Commuter 3 has entered the bridge at time : 5
Commuter 3 has an EZPass.
Commuter 3 has left the bridge and will head to work.
Commuter 3 has arrived at work. Time: 6
Commuter 5 has entered the boulevard. He entered lane : 0 at time: 6
Commuter 5 has entered the bridge at time : 6
Commuter 5 has an EZPass.
Commuter 5 has left the bridge and will head to work.
Commuter 5 has arrived at work. Time: 6
Commuter 7 has entered the boulevard. He entered lane : 1 at time: 6
Commuter 7 has entered the bridge at time : 6
Commuter 7 has an EZPass.
Commuter 7 has left the bridge and will head to work.
Commuter 7 has arrived at work. Time: 7
Commuter 9 has entered the boulevard. He entered lane : 2 at time: 7
There are double-parked cars in Commuter 9 lane. Time: 7
Commuter 9 is yielding until lane is clear.Time:7
Road Rage from Commuter 9. Speeding to get to work on time. Time: 7
I must stop since I'm nearing a red light, thinks Commuter 9.
Commuter 9 has entered the bridge at time : 7
Commuter 9 has an EZPass.
Commuter 9 has left the bridge and will head to work.
Commuter 9 has arrived at work. Time: 7
The threads are created and stored in an array in the main method of the Project 1 class. And here is my synchronized method:
Expand|Select|Wrap|Line Numbers
- synchronized void tollBooth(){
- System.out.println(Thread.currentThread().getName() +
- " Entered the toll booth. Value is: " + value);
- if(commuterNumber !=4){
- value --;
- if(value <0){
- System.out.println(Thread.currentThread().getName() +
- " Entered the waiting line. Value is: " + value);
- while(true){
- try{
- System.out.println(Thread.currentThread().getName()+
- " Will wait now.");
- wait();
- System.out.println(Thread.currentThread().getName() +
- " is breaking out the while.");
- break;
- }catch(InterruptedException e){
- if(value >= 0)
- break;
- else continue;
- }
- }
- }
- }
- System.out.println(Thread.currentThread().getName()+
- " Exited the line. Value: "+ value);
- value++;
- if(value <= 0){
- System.out.println(Thread.currentThread().getName() +
- " notifiying the next waiting thread. Value: "+ value);
- notify();
- }
- }