473,804 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

notify() and wait() problem

2 New Member
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 3490
JosAH
11,448 Recognized Expert MVP
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.conc urrent package which has many handy classes and methods available (pay special attention to the Semaphore class).

kind regards,

Jos
Jul 31 '09 #2
greyradio
2 New Member
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
13563
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? TIA, ves
1
2830
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, so maybe there is something else of wrong. Using the example 2 in the libpq chapter on "Postgres Programmer's Guide" as template i write :
2
4718
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 jobs and beeing able to be notified when each of these threads finished it's job.
18
1804
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 that is done, loading form does this: Me.Visible = False mainform.ShowDialog() Application.Exit() So far so good (is this a dumb way of doing that???). Now, I am
4
2211
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 notification from libpq (PQnotifies), each client proceeds to execute a query for the last five records in the timeclock table. SELECT * FROM timeclock ORDER BY touched DESC LIMIT 5;
4
7083
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 never could find any answers in the e-mail nor in the documentation. Perhaps I just missed it. I have tried the following code snipit: Connection db = DriverManager.getConnection(url, user, passwd);
5
7563
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) exceptions : (or u can download it from : http://www.geocities.com/mndt_0/files/WaitingForThread.java at this link it has the spaces )
3
1621
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
1631
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 do u attach an event handler to the context menu.i want to add event handlers to hide and show the media player. The notify icon appears in the tray when i run the application but does not disappear wen it is stopped.
0
9593
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7633
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.