Have thread in wait, but calling notifyAll() does nothing

I'm trying to learn how wait() and notify() work with Threads, and I've created the following classes to test them out. However, when I call notifyAll() to restart the other thread, my program just sits there. Doesn't exit or anything. Could someone please help me figure out what is happening?
Here is my code:
//my thread class, initializes variables and contains the methods to call wait, notify and run()
public class MyRunClass extends Thread{
public boolean A;
int startNum;
     int endNum;
     public MyRunClass(int y, String name){
          super(name);
          startNum = y;
     public synchronized void passStartNum(){
          endNum = 10;
          if(startNum > 5){
               startNum = 4;
               endNum = 10;
               try{
                    System.out.println(getName() +" going into wait " );
                    wait();
               }catch(InterruptedException a){
               System.out.println("falling out of wait");
          }else{
               endNum = 5;
     public synchronized void myloop(){
          while(startNum < endNum){
               System.out.println(getName() +" startNum: " + startNum +
"end Num: " + endNum);
               try{
                    Thread.sleep(100);
               }catch(InterruptedException e){}
               startNum++;
          if(getName().equals("Thread1")){
               System.out.println("going to do notify");
               notifyAll();
     public void run(){
          passStartNum();
          myloop();
//my class with the main() and the creation of my two threads
public class AThread{
     Thread myt;
     Thread myt2;
     public AThread(){
          myt = new Thread(new MyRunClass(1, "Thread1"));
          myt2 = new Thread(new MyRunClass(6, "Thread6")) ;
     public void startTh(){
          myt2.start();
          myt.start();
     public static void main(String args[]){
          AThread at = new AThread();
          at.startTh();
//Any help would be really appreciated!! Thanks!

The other responses are correct, but since this threads are a complicated topic, I'll add another rewording.
You can only call wait or notify on an object when you have that Object's monitor/lock, that is when you are synchronized on that object. ie...
Object lock = new Object();
synchronized (lock) {
    lock.wait();
}When a thread calls wait, it releases the lock, which enables another thread to grab the lock - possibly to call notify on the object, which would wake up a thread waiting on that object. (notifyAll wakes up every thread that is waiting on that object) Note that the newly-woken thread(s) still won't be able to run until the thread that called notify releases the lock, since they are still in a synchronized block.
In terms of design, if you want a single thread to wait and be woken it is appropriate to call wait and notify on the thread in question. If you are running multiple threads, and are waiting to synchronize between the threads, it is often more appropriate to call wait and notify on an object shared amongst the threads.
There are a barrage of articles and lessons on the net... try a search on thread monitors or synchronization.
-Troy

Similar Messages

  • I have an iPhone 4, but the computer does not not recognize, do not recharge the battery if it is off

    I have an iPhone 4, but the computer does not not recognize, do not recharge the battery if it is off

    Did you already try another USB cable or USB port?
    Did you try to reset the phone by holding the sleep and home button for about 10sec, until the Apple logo comes back again?
    Also try these tips as well:
    iOS: Device not recognized in iTunes for Windows

  • My phone has gone into recovery mode on its own.  says to connect to itunes.  i have restored it 10 times and it does nothing please help me i am out of the country and desparate

    my phone has gone into recovery mode on its own.  says to connect to itunes.  i have restored it 10 times and it does nothing please help me i am out of the country and desparate

    Open iTunes on computer. Connect iPhone to computer. If the iPhone is recognized in Recovery Mode, then Restore the Firmware. If not recognized, then hold both Home and Power Buttons until the iPhone is recognized in Recovery Mode. Usually takes about 20 seconds of holding both buttons. Restore the iPhone Firmware.

  • HT4623 im trying to update my phone and have goggled how to but my phone does not have the option to click on software update has anyone else had this problem and if so how did you go about it. thanks in advance.

    Im trying to update my iphone 4s as i can't dowload alot of thing as it says i need to update to IOS 5 i have googgled how to do it but my phone does not have software update option on it. does anyonw know how to fix this problem or had this problem? if so how did you go around fixing it and updating the phone. thanks in advance.

    gemsmobiletans wrote:
    my phone does not have software update option on it.
    If you have iOS 4... See Here...
    http://support.apple.com/kb/HT4972
    OR...
    Connect to iTunes on the computer you usually Sync with and “ Check for Updates “...
    If an Update Appears Install it... if not... you are up to date for your particular Device...
    See the Using iTunes Section Here...
    How to update your iPhone, iPad, or iPod touch
    Make sure you have the Latest Version of iTunes Installed
    iTunes free download from www.itunes.com/download
    Note:
    The Current iOS for the iPhone 4s is iOS 7.

  • I just purchased a new to me ibook g4 I have all the disks but the cd does not seem to read them properly. I wanted to re install everythMost everything works anyway except my address book application , help how do I reinstall

    I just purchased a new to me ibook g4. I have all the disks but they do not seem to work properly. When I restart holding the C key they just spit out of the drive. I wanted to reset everything back to factory and start over but no go. Most everything works anyway except my address book application. It seems to be missing. How can I download and reinstall a new address book App. Thanks, John

    iBook G4/1.33 12-Inch (Mid-2005 - Op)
    1.33 GHz PowerPC 7447a (G4)
    Intro Date:
    July 26, 2005
    Disc Date:
    May 16, 2006
    Order No:
    M9846LL/A
    Model No:
    A1133 (EMC N/A)
    Subfamily:
    iBook G4 - Mid-2005
    Model ID:
    PowerBook6,7
    Std. RAM:
    512 MB
    Std. VRAM:
    32 MB
    Std. HD:
    40 GB (4200 RPM)
    Std. Optical:
    8X "Combo Drive"
    Complete iBook G4/1.33 12-Inch (Mid-2005 - Op) Specs
    The problem with accessing the content of the disks is the CD Drive does not seem to access them. It also has a heck of a time ejecting them. Other disks seem to read. The unit came with imovie disks and I can use them. The disks look perfectly good no scratches but they just don't work, I was hoping I could just get a online copy of the address book to replace the one that seems to be missing. Or find it somewhere on my drive. Have no clue where to look though. 

  • I have downloaded latest itunes but my ipad does not show in devices when I try to sync.  Any suggestions?

    I have just gotten my new ipad!!!  I downloaded latest version of itunes but my ipad does not show in devices when I try to sync music.  Any suggestions??

    Does a little box come up seeing the iPad as a camera? That doesn't impact iTunes as much as it confirms that your computer sees the device. If your computer doesn't see it then iTunes doesn't know it's there.
    If you're plugged into a hub or extender, try plugging it directly into the box or main part of your computer.
    If you're using an extension, stop and just use the included sync cable.
    Try using a different USB port.
    After you installed iTunes, did you restart your computer?
    Usually what's needed is the 'apple device driver' which installs automatically, you just need to sometimes trigger the install, using different ports helps with that.
    If the simple trouble shooting doesn't help then you can search on the Apple Device Driver and there's more detailed trouble shooting on the site.

  • I HAVE ITUNES LOADED ON MY PC, BUT AS SOON AS I OPEN THE ITUNES FOLDER, IT COMES UP WITH A SMALL SCREEN SAYING THAT ITUNES HAVE A PROBLEM AND MUST CLOSE.  I HAVE RE-INSTALLED ITUNES BUT IT STILL DOES THE SAME. PLEASE TELL ME WHAT KIND OF PROBLEM IT CAN BE

    I HAVE ITUNES LOADED ON MY PC, BUT AS SOON AS I OPEN THE PROGRAM A POP-UP SCREEN APPEAR WHICH STATES THAT AN ERROR OCCURED IN ITUNES AND IT MUST CLOSE.
    I HAVE RE-INSTALLED ITUNES, BUT THAT HAS NOT HELPED.
    CAN SOMEBODY PLEASE EXPLAIN TO ME HOW TO SOLVE THIS PROBLEM?
    LEILANI178

    Leilani ~ Welcome to the Support Communities. Before posting your question it's worth searching the Communities — here's an Apple tutorial:
    Apple Support Communities: Searching
    ...But since you've already posted, look in the More Like This section to the right of your post. Also:

  • HT3917 I cannot get my mouse to work, I have changed the batteries, but it still does not register any ideas???

    I cannot get my mouse to work, I have changed the batteries and it still does not work any ideas???????????

    Hello:
    Try resetting the SMC and PRAM.
    Barry

  • Trying to download Trial of Photoshop CS5.1 but it is doing nothing!

    Dear Adobe
    I am currently trying to download the trial version of your Photoshop CS5.1 Extended and have tried to download the Adobe CS5 Standard but when I down load these they have the same reaction. They do nothing.
    I have been downloading, uninstalling and reinstalling the Adobe Download Assistant and tried doing the whole process again. But there is still no success.
    At the start it all looks fine, it asks me to sign in to my Adobe ID to continue, I do so. Then it asks me to choose a location to put the file. I do so.
    Then it says Welcome, Michelle on the top right corner. And does nothing. At the bottom of the download ribbon it still says Sign in with your Adobe ID to continue, but the thing is I already have.
    I have even given it half an hour to sort itself out and do something, but it still hasnt.
    I am on a Windows Vista Operating system. My laptop seems to be making sounds like its doing something....but from the looks of things there is no progress.
    Someone please help me! :s
    ThanKS,
    Michelle x

    Michelle are you using a software firewall which might be preventing the Adobe Download Assistant from contacting the internet?
    Beyond that I would recommend trying to initiate the download through a different web browser after removing the Adobe Download Assistant.

  • Hello can you advise me what I can do when some sites have adobe flash reader but iPad 2 does not support it

    Hello is there anything I can do when ipad2 does not allow adobe flash reader
    THanks

    These are user-to-user forums, Apple don't monitor these forums (there are too many forums/threads/posts for that to happen).
    For Apple's thoughts on flash : http://www.apple.com/hotnews/thoughts-on-flash/
    But as Adobe have never managed to make a version of their flash player that works on iOS devices, and they have recently announced that they are stopping development on all mobile versions of it, it doesn't look like it will ever be supported on the iPad.

  • I have installed flash player but the computer does not recognise it

    I have installed flash player 11 and checked it is installed on my compuer.
    But if I try to play any media it says I need to install flash player.
    I went to your website at http://helpx.adobe.com/flash-player.html and got this message.
    I have tried uninstalling flash player and then trying again, restarting computer etc. But the computer will not play any media in IE.
    If you go to the control panel the player is installed. But thigs like iplayer do not recognise it as being installed.

    The message is clear: "Flash Player is either not installed or not enabled".  Did you check if it's enabled:
    http://forums.adobe.com/thread/885448
    http://forums.adobe.com/thread/867968

  • HT4098 How do you turn off an auto renewing subscription that does not show up within your account?  I applied for BBC iPlayer but wish to cancel the subscription, I have followed advise above but my account does not have a subscription section within set

    I have two Apple ID's as I am currently in Italy and when I first set up  my Apple ID it acknowledged my current location.  However when I applied for BBCiplayer I had to set up a different ID with UK address.  I thought maybe I was looking at the wrong account so used the e-mail address to which the auto renew receipt had been sent and followed the "forgotten Apple ID" link which prompted me for a new password.  Set up new password but whenever I try and log into account I am not asked for password and ID it just automatically takes me there (so unable to switch accounts). 

    What do you mean 'not asked for password and ID it just automatically takes me there' ?
    If you are using your computer's iTunes then you can log out of an account via the Store > Sign Out menu option, that will then change to a Store > Sign In option
    Is that what you are doing ?

  • HT1349 My  ipod touch 4th gen, will not let me access facebook or you tube it just takes ages to load then fails.  I have reset the ipod but its still doing the same.  Its only two months old, does anyone know how to fix it?

    My sons ipod touch has stopped letting him access facebook or you tube.  On entering the app or going in via safari it just states loading for ages then fails, I have reset it for him, but it hasn't helped.  Is this a common problem?  It is only 2 months old.

    Try:
    - iOS: Not responding or does not turn on
    - Also try DFU mode after try recovery mode
    How to put iPod touch / iPhone into DFU mode « Karthik's scribblings
    - If not successful and you can't fully turn the iOS device fully off, let the battery fully drain. After charging for an least an hour try the above again.
    - Try on another computer
    - If still not successful that usually indicates a hardware problem and an appointment at the Genius Bar of an Apple store is in order.
    Apple Retail Store - Genius Bar       
    also
    Not Charge
    - See:      
    iPod touch: Hardware troubleshooting
    - Try another cable. Some 5G iPods were shipped with Lightning cable that were either initially defective or failed after short use.
    - Try another charging source
    - Inspect the dock connector on the iPod for bent or missing contacts, foreign material, corroded contacts, broken, missing or cracked plastic.
    - Make an appointment at the Genius Bar of an Apple store.
      Apple Retail Store - Genius Bar 

  • I have purchased a song, it is in my library on my laptop. I have syncd several times but the song does not go to my ipod.

    I have purchased a song, it is in my library but does not show on my ipod. I have syncd several times. Can anyone help please?

    Are you following the right instructions?
    iTunes 11 for Windows: Set up syncing for iPod, iPhone, or iPad
    Do the songs play in iTunes on the computer?
    Have you successfully synced music from the iTunes library to your iPod before?

  • Getting the Entity does not have a key defined but it actually does.

    So I am trying to use EF 6 with the AdventureWorks database to build out some web api services.
    Everything was going well until I added the EmployeeDepartmentHistory table and then I keep getting the  "The entity "EmployeeDepartmentHistory' does not have a key defined."
    But here is my edmx file which does have it defined.
    edmx:
    <EntityType Name="EmployeeDepartmentHistory">
    <Key>
    <PropertyRef Name="BusinessEntityID" />
    <PropertyRef Name="DepartmentID" />
    <PropertyRef Name="ShiftID" />
    <PropertyRef Name="StartDate" />
    </Key>
    <Property Name="BusinessEntityID" Type="int" Nullable="false" />
    <Property Name="DepartmentID" Type="smallint" Nullable="false" />
    <Property Name="ShiftID" Type="tinyint" Nullable="false" />
    <Property Name="StartDate" Type="date" Nullable="false" />
    <Property Name="EndDate" Type="date" />
    <Property Name="ModifiedDate" Type="datetime" Nullable="false" />
    </EntityType>
    <EntityType Name="EmployeeDepartmentHistory" Abstract="false">
    <Key>
    <PropertyRef Name="BusinessEntityID" />
    <PropertyRef Name="DepartmentID" />
    <PropertyRef Name="ShiftID" />
    <PropertyRef Name="StartDate" />
    </Key>
    <Property Name="BusinessEntityID" Type="Int32" Nullable="false"/>
    <Property Name="DepartmentID" Type="Int16" Nullable="false" />
    <Property Name="ShiftID" Type="Byte" Nullable="false" />
    <Property Name="StartDate" Type="DateTime" Nullable="false" Precision="0" />
    <Property Name="EndDate" Type="DateTime" Precision="0" />
    <Property Name="ModifiedDate" Type="DateTime" Nullable="false" Precision="3" />
    <NavigationProperty Name="Department" Relationship="AdventureWorks2012Model.FK_EmployeeDepartmentHistory_Department_DepartmentID" FromRole="EmployeeDepartmentHistory" ToRole="Department" />
    <NavigationProperty Name="Employee" Relationship="AdventureWorks2012Model.FK_EmployeeDepartmentHistory_Employee_BusinessEntityID" FromRole="EmployeeDepartmentHistory" ToRole="Employee" />
    <NavigationProperty Name="Shift" Relationship="AdventureWorks2012Model.FK_EmployeeDepartmentHistory_Shift_ShiftID" FromRole="EmployeeDepartmentHistory" ToRole="Shift" />
    </EntityType>
    Please help I've been stuck on this for days. I've tried deleting it and re-adding it but to no avail.

    EmployeeDepartmentHistory and this table itself has a primary key on it?

Maybe you are looking for

  • The sound has stopped working on my iPhone since I downloaded iOS 6

    Am I missing some thing or did I not finish it properly. Since the new software has been installed there is no sound !!!! Help

  • 9.2.0.6 Intersection Error

    We’ve recently upgraded to 9.2.0.6 as part of a major release and we’re seeing the following spatial error intermittently in our Production system which is causing us major problems. When we get this problem the whole GIS application grinds to a halt

  • Help with connecting iPod

    Ok I have the iPod with Dock version and its in good condition. I had about 500 songs on it and didnt connect it to my computer for about 6 months because I never really got any new songs. In between that time, I upgraded iTunes to the latest version

  • Mail address incorrect in Mail when part of a Group but correct in AB

    Is there a way to remove previous addresses that have been received in Mail? One of my frequent contacts shows an incorrectly formatted address when I create a new message. It shows the name and the email address but it does not include the first < o

  • How to output action parameters

    Hello Experts! What should I do to output Action Parameters to the filesystem in GP? Can this be done? Thanks & Regards