473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

notify() and wait() problem

I've recently have been given an assignment to do and it seems that notify() does notify() any of the waiting threads. 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 someone can point out what I'm doing wrong with the synchronized method. I've attached the java files. Thanks.

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
  1.  
  2. synchronized void tollBooth(){
  3.  System.out.println(Thread.currentThread().getName() +
  4.         " Entered the toll booth. Value is: " + value);
  5.  if(commuterNumber !=4){
  6.     value --;
  7.     if(value <0){
  8.             System.out.println(Thread.currentThread().getName() +
  9.                 " Entered the waiting line. Value is: " + value);
  10.         while(true){
  11.             try{
  12.                 System.out.println(Thread.currentThread().getName()+ 
  13.                         " Will wait now.");
  14.                wait();
  15.  
  16.           System.out.println(Thread.currentThread().getName() +
  17.                " is breaking out the while.");
  18.               break;
  19.        }catch(InterruptedException e){
  20.            if(value >= 0) 
  21.                break;
  22.            else continue;
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.    System.out.println(Thread.currentThread().getName()+
  28.            " Exited the line. Value: "+ value);
  29.    value++;
  30.     if(value <= 0){
  31.        System.out.println(Thread.currentThread().getName() +
  32.            " notifiying the next waiting thread. Value:  "+ value);
  33.                  notify();
  34.             }
  35.  
  36.         }
  37.  
  38.  
  39.  
Jul 31 '09 #1
2 3442
JosAH
11,448 Expert 8TB
I suspect you're synchronizing on the wrong object(s). A synchronized method synchronizes on the object for which the method is a member. If you have 10 different objects they synchronize on these 10 different objects; if they enter a wait state no other thread can notify them because all threads synchronized on a different object. Also have a look at the java.utils.concurrent package which has many handy classes and methods available (pay special attention to the Semaphore class).

kind regards,

Jos
Jul 31 '09 #2
Thanks I see what I was doing wrong. It works now.
Aug 1 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Vespasian | last post by:
notify() and wait() throw an exception if the calling thread does not own the object's monitor. Does this imply that the check is made at runtime, and if so, why can't it be made at compile time? ...
1
by: Alessandro GARDICH | last post by:
Hi to all I have problem with NOTIFY/LISTEN ... I'm writing a C++ application with libpqxx, I thought was a problem of the lib but I try also with a C program using libpq with the same result,...
2
by: objectref | last post by:
hi to all folks, i want to create the following senario: i have a T1 (Thread 1) that acts like http server that receives incoming requests. From that T1, i want to spawn from T1 to Tn thread...
18
by: Derek Martin | last post by:
Hi there, this is probably really dumb, but I am using a dialog as my main form and my startup form is my splash screen - which implements some progress bars to load up some relevant data. When...
4
by: Joe Lester | last post by:
I'm using PostgreSQL 7.4.1. I have 140 clients connected on average using libpq. When one client sends "NOTIFY timeclock;" to the server all 140 clients are listening for it. After receiving a...
4
by: Glenn Sullivan | last post by:
Hi, I have been trying to get LISTEN/NOTIFY working in with JDBC. I cannot seem to get notified. I looked in the e-mail archive and saw a lot of similiar questions a couple of years ago. I...
5
by: cozsmin | last post by:
hello , as u know wait() and notify() will not thow an exception if the method that calls them has the lock , or esle i misundrestood java :P this is the code that throws (unwanted) ...
3
by: Lateef | last post by:
i am learning java. in multithreading i want to put a thread to wait() and then call back it by notify() .. but i am not able to do so. can any one explain it to me by simple example..
1
by: namrataa | last post by:
i have a notify icon for our media player developed in wpf. included files are notify.cs,notify.resx,notify.designer.cs. i have also added a class file called app.cs . now the problem is how...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.