Results 1 to 2 of 2
Thread: wait() and notify() problems
- 07-31-2009, 10:57 AM #1
Member
- Join Date
- Jul 2009
- Posts
- 1
- Rep Power
- 0
wait() and notify() problems
I've recently have been given a project to do and it seems that notify() is missed. The project entails 10 commuters and two different toll booths. The EZPass booth allows a commuter to pass easily while a cash booth creates a wait line. The wait line is expected to wait for the 5th commuter to pass before any commuter on the wait line can pass through the toll booth. This is where my problem lies. All the commuters wait for the 5th commuter thread to pass, however when the 5th commuter does notify(), none of the commuter threads receive it. I've spend hours trying to solve this problem but I haven't has any results. Hopefully a pair of fresh eyes can point out what I'm doing wrong with the synchronized method.
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:
Java Code:synchronized void tollBooth(){ System.out.println(Thread.currentThread().getName() +" Entered the toll booth. [INDENT]Value is: " + value);[/INDENT] if(commuterNumber !=4){ value --; if(value <0){ System.out.println(Thread.currentThread().getName() + " Entered the [INDENT][INDENT]waiting line. Value is: " + value);[/INDENT][/INDENT] while(true){ try{ System.out.println(Thread.currentThread().getName()+ " Will [INDENT][INDENT][INDENT]wait now.");[/INDENT][/INDENT][/INDENT] wait(); System.out.println(Thread.currentThread().getName() +" is [INDENT][INDENT][INDENT]breaking out the while.");[/INDENT][/INDENT][/INDENT] break; }catch(InterruptedException e){ if(value >= 0) break; else continue; } } } } System.out.println(Thread.currentThread().getName()+" Exited the line. Value: [INDENT][INDENT]"+ value);[/INDENT][/INDENT] value++; if(value <= 0){ System.out.println(Thread.currentThread().getName() +" notifiying the [INDENT][INDENT]next waiting thread. Value: "+ value);[/INDENT][/INDENT] notify(); } }
As you can see when Commuter 4, the commuters are numbered 0 - 9, notify() goes unnoticed by Commuter 0 who is waiting at the cash booth. I've also attached my java files. Thanks for your time.
- 08-03-2009, 03:36 AM #2
I don't have time to study the code in-depth, but I noticed that wait() and notify() do not specify a specific Object. All the threads must synchronize, wait(), and notify() on the same *instance* of an Object to interact. Either start all your threads against one instance of the class containing the method, or create one Object that you pass to each instance and use that for explicit synchronized(), wait(), and notify() calls. I prefer the latter method, because it is more explicit, and I don't have to worry about multiple threads updating the fields of a single instance.
Similar Threads
-
Make a program notify
By McChill in forum New To JavaReplies: 7Last Post: 06-03-2009, 09:17 PM -
about wait() and notifyALL
By denis in forum Threads and SynchronizationReplies: 13Last Post: 04-22-2009, 08:28 AM -
Thread Wait
By jiexx in forum Threads and SynchronizationReplies: 1Last Post: 03-19-2009, 05:26 PM -
What is the execution path of wait() and notify() ?
By AegisCruiser in forum Threads and SynchronizationReplies: 1Last Post: 04-23-2008, 06:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks