Why does it seem like notify() acts like notifyAll() for threads?

I'm running a little exercise here:
class Reader extends Thread {
      Calculator c;
       String name ="Fred";
       static int i = 0;
      public Reader(Calculator calc) {
         c = calc;
         name = "Fred"+(i++);
      public void run() {
         synchronized(c) {
           try {
              System.out.println(name + " is waiting for calculation...");
              c.wait();
           } catch (InterruptedException e) {}
           System.out.println("Total is: " + c.total + " for "+name);
     public static void main(String [] args) {
        Calculator calculator = new Calculator();
        new Reader(calculator).start();
        new Reader(calculator).start();
        new Reader(calculator).start();
        try {
             Thread.sleep(1000);
          } catch (InterruptedException e) {
               System.out.println("Got interrupted");
        calculator.start();
  class Calculator extends Thread {
     int total;
     public void run() {
        synchronized(this) {
           for(int i=0;i<100;i++) {
              total += i;
           System.out.println("Notifying start");
           notify();
           System.out.println("Notifying end");
  }the output is
Fred0 is waiting for calculation...
Fred1 is waiting for calculation...
Fred2 is waiting for calculation...
Notifying start
Notifying end
Total is: 4950 for Fred0
Total is: 4950 for Fred2
Total is: 4950 for Fred1
I expect only one "Total is: ..." output to come out since I expect only one thread to wake up from its waiting state from that notify() call. However all three threads wake up. What am I misunderstanding? I thought a thread waits forever unless you pass a time parameter into wait()... or am I wrong?
Edited by: lapchern on Nov 17, 2009 12:53 PM

Well, first of all, don't discard Exceptions and then wonder about the behaviour of a program :-).
But that's not the issue here. From some quick testing, I didn't get the same behaviour when I ran using wait/notify on a lock object, not the thread itself:
public class WaitNotify extends Thread {
      Calculator c;
     String name ="Fred";
     static int i = 0;
      public WaitNotify(Calculator calc) {
         c = calc;
         name = "Fred"+(i++);
      public void run() {
         synchronized(c.lock) {
           try {
              System.out.println(name + " is waiting for calculation...");
              c.lock.wait();
           } catch (InterruptedException e) {
              e.printStackTrace();
           System.out.println("Total is: " + c.total + " for "+name);
     public static void main(String [] args) {
        Calculator calculator = new Calculator();
        new WaitNotify(calculator).start();
        new WaitNotify(calculator).start();
        new WaitNotify(calculator).start();
        try {
         Thread.sleep(1000);
      } catch (InterruptedException e) {
         System.out.println("Got interrupted");
        calculator.start();
  class Calculator extends Thread {
     int total;
     final Object lock = new Object();
     public void run() {
        synchronized(lock) {
           for(int i=0;i<100;i++) {
              total += i;
           System.out.println("Notifying start");
           lock.notify();
           System.out.println("Notifying end");
  }Not sure ATM why that is, but it's I think best practice not to lock on the Thread itself and instead a different Object, so there's a fix. It'll give you a place to start investigating.
Edit: Might be good to look at the [Javadoc for Object.wait()|http://java.sun.com/javase/6/docs/api/java/lang/Object.html#wait()]:
...interrupts and spurious wakeups are possible, and this method should always be used in a loop:
     synchronized (obj) {
while (<condition does not hold>)
obj.wait();
... // Perform action appropriate to condition
Edited by: endasil on 17-Nov-2009 4:08 PM
Edited by: endasil on 17-Nov-2009 4:09 PM

Similar Messages

  • Why does my exported PDF not look like the original?

    When I export a PDF to excel, the excel document doesn't look like the original. Any idea why or how to make it an exact duplicate?

    No discussion mi adob france
    Acrobat Pro, PDF Pack, Export PDF & More | A...
    Acrobat
    Adobe Document Solutions make it simple to help you get more done every day, and lets you easily create, edit, send, an...
    Envoyé depuis Molto pour iPad
    Message transféré:
    De: Allamelle
    Envoyé: jeudi, février 12, 2015 10:53 AM
    À: [email protected]
    Objet: Re:  Why does my exported PDF not look like the original?
    Stop Mdr
    Envoyé depuis Molto pour iPad
    De: Test Screen Name
    Envoyé: jeudi, février 12, 2015 10:31 AM
    À: René Allamelle
    Objet:  Why does my exported PDF not look like the original?
    Why does my exported PDF not look like the original?
    created by Test Screen Name in Adobe Acrobat.com Services - View the full discussion
    "Vaguely like" is the best we can hope for and a minor miracle in itself. PDF was never meant to be converted.
    If the reply above answers your question, please take a moment to mark this answer as correct by visiting: https://forums.adobe.com/message/7187391#7187391 and clicking ‘Correct’ below the answer
    Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page:
    Please note that the Adobe Forums do not accept email attachments. If you want to embed an image in your message please visit the thread in the forum and click the camera icon: https://forums.adobe.com/message/7187391#7187391
    To unsubscribe from this thread, please visit the message page at , click "Following" at the top right, & "Stop Following"
    Start a new discussion in Adobe Acrobat.com Services by email or at Adobe Community
    For more information about maintaining your forum email notifications please go to https://forums.adobe.com/thread/1516624.

  • Why does ipad has the same ios like the Iphone4s ,it should be different than iphonewhy does ipad has the same ios like the Iphone4s ,it should be different than iphone

    why does ipad has the same ios like the Iphone4s ,it should be different than iphone

    Having one iOS that works across several platforms is one of the advantages of the iOS system. This allows developers to produce one software product instead of several and reduces development time and costs. This is a fundamental difference between iOS and Android operating systems where operating system varieties impede development.

  • Why does Lion seem slower than Snow Leppard?

    Why does Lion seem slower than Snow Leppard?

    michaelfromrice lake wrote:
    Why does Lion seem slower than Snow Leppard?
    It's anyone's guess.  Startup and shutdown as well as application launches take longer in Lion than they did in Snow Leopard on the three Macs that I "upgraded," and I also see the spinning beach ball more often than I used to.  Add to that the HDMI-related crashes on my Mac mini...
    Quite frankly, I have yet to see where I have gained any advantages after upgrading to Lion, and I definititely have problems that I didn't have under Snow Leopard.  Of course, all of it must be my fault, as Apple would never ever relase an upgrade that was less than perfect.

  • How do I delete documents from my iPad that were added when it ran regular Acrobat, now that it has switched to DC?  The docs are not on the cloud, and DC does not seem to have a delete function for non-cloud docs.

    I use an iPad.  It automatically switched me from old-fashioned Acrobat to DC.  How do I delete docs that were put on my iPad with the old Acrobat?  They are not in the cloud, and DC does not seem to have a delete function for them.

    Hi,
    By default, Acrobat DC for iOS displays recently viewed files.  You need to switch to other file location (such as Local, Document Cloud, Creative Cloud) to delete, rename, move, or duplicate files.
    You can switch to Local, if you would like to see the files and folders that are locally stored on your iPad.
    Would you take a look at the following document to see how you can switch to other file location and delete files?
    How to manage files in Acrobat DC for iOS
    Please let us know if you have additional questions.  Thank you.

  • I cannot send an email from my iPad 2? No problem receiving, why does this happen? Have tried the suggestions for setting up email and after doing the sync mail through iTunes receiving worked great but still cannot send? Any help would be great

    I cannot send an email from my iPad 2? No problem receiving, why does this happen? Have tried the suggestions for setting up email and after doing the sync mail through iTunes receiving worked great but still cannot send? Any help would be great!

    The fact that you can receive means you have a valid e mail address, and have established the connection to the incoming server, so all of that works.  Since the send does not work, that means your outgoing server is rejecting whatever settings you used formthe outgoing set up.  Try them again. 
    Google your particular isp, and ipad and many times you will find the exact settings needed for your isp.  Or tell us here, and soneone else may be on the same isp.  Some mail services need you to change a port, or have a unique name for the outgoing server.  
    Kep trying.

  • Why does my IPad not sync with my IPhone for Ibooks bookmarks?

    Why does my IPad not sync with my IPhone for IBooks bookmarks?  Both Pad and Phone have sync turned on.

    I must apologize as I misread your post. I though that you were talking about Safari bookmarks but you post does clearly state iBooks bookmarks.
    You may want to just try restarting both devices and see if that helps. One other thing that you could try is quit iBooks on both devices and then launch it again and see if the bookmarks are synced when you relaunch.

  • Why does my iPad 4 keep update requested status for ios8?

    Why does my iPad 4 keep update requested status for ios8?

    The Apple servers are slammed right now with iOS 7 downloads AND activation requests.
    Be patient and try again later.

  • How do I adjust double click speed in Mountain Lion. The mouse system preferences does not seem to have a speed slider for double clicking. Only the tracking speed slider is visible.

    Double clicking is no longer opening my folders.
    How can I adjust the double click speed in Mountain Lion to reduce the double clicking speed. The mouse system preferences does not seem to have a speed slider for double clicking. Only the tracking speed slider is visible.

    For 10.8.3,  it's in System Preferences > Accessiblity > highlight Mouse & trackpad on the left.  The Double click speed slider shows on the right.  Moving it to the left gives you more time between clicks.  Moving it right gives you less time.

  • Why does mac mini with snow leopard download skype for windows instead of mac

    why does mac mini with snow leopard download skype for windows instead of mac ?

    Are you using this link?
    http://www.skype.com/en/download-skype/skype-for-mac/

  • Does iCloud Make Pop Mail Act like IMAP?

    I have basically 1 main question, but 2 different scenarios I'm dealing with - one personally for my mail and the other for my boss.
    Though a longtime Mac user, I am new to using Mountain Lion, iPhone, iPad and iCloud and historically I have always had numerous email accounts in Mac Mail set up as POP and countless elaborate folders that I file and keep all my old emails in for reference.
    When buying my new iMac with ML, the one thing I have not yet done is turn on iCloud for mail.
    If I am understanding how this works (before I do it and create a nightmare of duplication on my iPhone & iPad, etc), if I continue to create my email accounts as POP on my new ML iMac, with their various folders, etc., and turn on iCloud for mail, will it simply reflect on my other devices all the same email accounts and folders and in essence act like an IMAP account in that if you move or delete a piece of mail on one device it will do so on all the devices?
    I not only need to sort this on my computer, but my boss'.  She currently only uses gmail and has all these same devices but they are set up as IMAP and iCloud is turned on and she has mass duplication that is unbearable.
    I am thinking it would be best for me to make her gmail account set up as a POP account and keep iCloud turned on and it will basically eliminate the gmail replication but still allow her devices to all be in sync and have action on one device be seen on all the others.
    This would allow me to create folders for her on her computer in Mac Mail and not use the folder system in gmail at all.
    Am I understanding this correctly?  Hope I'm making sense!
    Thanks so much!

    Hi Glenn:
    Thanks for your response.  I do understand what IMAP does, and I do purposely want all my emails POP so they are downloaded as localized on my computer.
    But where the confusion is for me is iCloud.
    There is "iCloud mail" which I DO NOT WANT.  I don't need yet another mail account.
    I mean "turning on" iCloud for your existing mail.  The way you can turn it on for Calendar, Contacts etc. in Systme Preferences>iCloud> check Mail.  Right now I do not have this checked on my iMac and I actually set up only one of my email accounts on my iPhone as a POP.
    I was hoping that if I organize my email POP accounts the way I want on my iMac and file the various emails into whatever folders, that if I "turned on" iCloud, that it would reflect the same on my phone.  If not, I will not add these accounts to my phone, as I don't want all that on my phone. 
    I like being able to glance at an email on the phone if I'm in town and be able to delete it, but still know its waiting for me to actually deal with and/or file when I get back to my main computer.
    I monitor over 100 email accounts, some mine, but many for business clients and this is why I don't want my phone clogged with all this.  But if turning on iCloud would mimic my iMac mail, then I could just file the emails on the Mac and know they are there in the phone (and I wouldn't delete them on the phone, as it would delete them on the Imac).  Hope this is making sense!
    In my boss' case, she has the reverse issue.
    Someone in the UK set up her iPad, iPhone and laptop with Gmail - her only email account.
    It is IMAP.  They also turned on iCloud (not @icloud mail) and what she has is not only the duplicates caused by Gmail IMAP settings (which you have to remove in Gmail Settings so you don't have the dual Inbox and Gmail "All Mail" inbox on your devices, but to make things more complex for her, she has a Portuguese iPhone and a US iPhone (rather than just swapping SIM cards).
    What I hear all day long is "why do I have all these duplicate emails"  "where did everything go I was just looking at?"  yadda yadda yadda.
    She has her Inbox which has 2000 unread emails sitting in it and her "All Mail" with even more.  Its a mess I want to clean up but don't want to do the wrong thing without fully understanding here what would happen.
    I was hoping that "turning on" iCloud for your MacMail would actually reflect on your devices the same folders and accounts, in which case I could change her Gmail to a POP account and make folders for her and simply have them reflected on her devices once.
    But is turning on iCloud in Syst. Pref for Mail doesn't do that, it won't work.
    Thanks!!!!

  • Why wont my itunes open it acts like its loading but then it just never comes up

    itunes acts like its loading but never comes up and rarely when it does come up it freezes.

    Hello, jessykins25.
    Thank you for the question.  This sounds like you will need to rebuild your iTunes library file.  Below, I have included the article that walks you through this process.  Start with the section labeled Re-creating the iTunes Library file.
    iTunes: How to re-create your iTunes library and playlists
    http://support.apple.com/kb/ht1451
    Cheers,
    Jason H.

  • Why does my xfce4-panel taskbar look like this?

    Why does one task take up the whole bar? It's so ugly!!

    Ah, so this brings up another aesthetically displeasing trait: Now my panel changes size depending on how many tasks are in my taskbar because I have it set to normal width, rather than full width. Is there any way to force the whole panel to stay at one length?

  • Why does my iPhone keep notifying me of upcoming birthdays on Facebook, I can't stand it. How do I get this off?

    Why does my iPhone keep posting notices about upcoming birthdays from fb? How do I get rid of it?

    It's all coming from Facebook.
    Either a notification is turned on or somehow these "events" have been synced to the calendar.
    The "how to fix" would depend on the notification.  If it's coming from the Facebook app, again, fix the notifications in the settings for your facebook account.  Since you're also getting emails, I believe this is where the notifications are coming from.
    If they are coming from the calendar app, then either change the notifications for each event or delete the events.

  • Why does the screen say, "Adobe Reader is blocked for this website."

    When I try to continue on to the site and trust the site, I recieve a different message stating, "Before viewing PDF documents, in this browser, you must launch Adobe Reader and accept the end user agreement, then Quit and relaunch the browser." 
    I have tried redownloading the program and accepting the agreement again, but this does not seem to be working.  I am out of ideas......

    Did you actually read the message you got?  It doesn't say anything about redownloading; it says to launch (open) the Adobe Reader application and accept the EULA.

Maybe you are looking for

  • Mapping of columns from 11i to R12 0 - AP Bank Accounts

    Hi All, We are working on application migration from 11i to R12. In our report columns bank_account_uses_id, customer_site_use_id and primary_flag from table AP_BANK_ACCOUNT_USES_ALL are used. Can some one help me to find how these columns are mapped

  • Firefox 6.0.2 crashes when I attempt to save a pdf

    When I have a pdf in my firefox browser and I click to save it, as soon as I click on the "UP" folder on the screen to navigate to the folder I wish to save the pdf in, an hourglass appears and firefox stops responding. I can't close firefox or open

  • How to recreate iFS context index on 8.1.7?

    Wanting to search the content on all the new text files uploaded to iFS... I ran sync_index on the iFS GLOBALINDEXEDBLOB_I index without first increasing the appropriate Datafile and Tablespace sizes first (thinking they would grow as needed). The sy

  • How do I make all clips the same brightness?

    I'm editing a four-minute video with ten clips, opening and closing titles, and a graphic. It's essentially a talking head presentation. The sound from a lapel mic is good as is the framing. But we used available lighting coming through a large windo

  • Using textField input as parameter to SQL Query

    Hi, Only a newbie can ask a quetion like this. I need my user to input a value in a text box, click a button and see the results of the Query displayed. I can do that if I bind a Dropdown box to a table, but since the table is huge, my page will slow