How does this thread behave?

CubbyHole class
public class CubbyHole {
     Object item;
     public synchronized Object get()
               throws InterruptedException {
          while(item == null)
               wait();
          Object i = item;
          item = null;
          notifyAll();
          return i;
     public synchronized void put(Object i)
               throws InterruptedException {
          while(item != null)
               wait();
          item = i;
          notifyAll();
}and
TestCubbyHole
public class TestCubbyHole {
     public static void main(String[] args) {
          Thread mainThread = Thread.currentThread();
          CubbyHole c = new CubbyHole();
          new CubbyWatcher(1, c);
          new CubbyWatcher(2, c);
          new CubbyWatcher(3, c);
          new CubbyWatcher(4, c);
          new CubbyWatcher(5, c);
          new CubbyWatcher(6, c);
          for(int ii=0 ; ii<args.length ; ii++) {
               new CubbyFiller(args[ii], c);
          try {
               System.in.read();
          } catch (java.io.IOException ioe) {
     static class CubbyWatcher implements Runnable {
          int id;
          CubbyHole theCubbyHole;
          public CubbyWatcher(int id, CubbyHole c) {
               this.id = id;
               theCubbyHole = c;
               System.out.println("CubbyWatcher");
               Thread t = new Thread(this);
               t.setDaemon(true);
               t.start();
          public void run() {
               while(true) {
                    try {
                         Object o = theCubbyHole.get();
                         System.out.println("Watcher #" + id +
                                   " got: " + o);
                    } catch (InterruptedException ie) {
     static class CubbyFiller implements Runnable {
          Object item;
          CubbyHole theCubbyHole;
          public CubbyFiller(Object i, CubbyHole c) {
               item = i;
               theCubbyHole = c;
               System.out.println("CubbyFiller" + ", item = " + item);
               Thread t = new Thread(this);
               t.setDaemon(true);
               t.start();
          public void run() {
               Thread me = Thread.currentThread();
               //int waitTime = (int)(Math.random() * 1000);
               try {
                    //me.sleep(waitTime);
                    theCubbyHole.put(item);
               } catch (InterruptedException ie) {
}In TestCubbyHole's CubbyFiller's run() method, if let the me.sleep uncommented, the output will be something like
parameter: aaa bbb ccc ddd eee
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyFiller, item = aaa
CubbyFiller, item = bbb
CubbyFiller, item = ccc
CubbyFiller, item = ddd
CubbyFiller, item = eee
Watcher #1 got: ddd
Watcher #2 got: ccc
Watcher #3 got: bbb
Watcher #4 got: aaa
Watcher #5 got: eee
where the last 4 lines (Watcher #s) are quite random. I understand that.
However, if with the code lines commented, I expect the out put to be:
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyFiller, item = aaa
CubbyFiller, item = bbb
CubbyFiller, item = ccc
CubbyFiller, item = ddd
CubbyFiller, item = eee
Watcher #1 got: aaa
Watcher #2 got: bbb
Watcher #3 got: ccc
Watcher #4 got: ddd
Watcher #5 got: eee
but however in fact it is:
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyFiller, item = aaa
CubbyFiller, item = bbb
CubbyFiller, item = ccc
Watcher #1 got: aaa
CubbyFiller, item = ddd
CubbyFiller, item = eee
Watcher #2 got: bbb
Watcher #3 got: ccc
Watcher #4 got: ddd
Watcher #5 got: eee
Can any one explain why??
thanks

I think this is the right code. For a single item queue like your CubbyHole, a user's sleep() should have no effect on the overall program execution.
/* queue: CubbyHole.java is same as before */
/* users:               */
public class TestCubbyHole {
  public static void main(String[] args) {
    Thread mainThread = Thread.currentThread();
    CubbyHole c = new CubbyHole();
    new CubbyWatcher(1, c);
    new CubbyWatcher(2, c);
    new CubbyWatcher(3, c);
    new CubbyWatcher(4, c);
    new CubbyWatcher(5, c);
    new CubbyWatcher(6, c);
    for(int ii = 0 ; ii < args.length; ii++) {
      new CubbyFiller(args[ii], c);
    try {
      System.in.read();
    } catch (java.io.IOException ioe) {
  static class CubbyWatcher implements Runnable {
    int id;
    CubbyHole theCubbyHole;
    public CubbyWatcher(int id, CubbyHole c) {
      this.id = id;
      theCubbyHole = c;
      System.out.println("CubbyWatcher");
      Thread t = new Thread(this);
      t.setDaemon(true);
      t.start();
    public void run() {
      while(true) {
        try {
synchronized(theCubbyHole){
          Object o = theCubbyHole.get();
          System.out.println("Watcher #" + id +
              " got: " + o);
        } catch (InterruptedException ie) {
  static class CubbyFiller implements Runnable {
    Object item;
    CubbyHole theCubbyHole;
    public CubbyFiller(Object i, CubbyHole c) {
      item = i;
      theCubbyHole = c;
//     System.out.println("CubbyFiller" + ", item = " + item);
      Thread t = new Thread(this);
      t.setDaemon(true);
      t.start();
    public void run() {
      Thread me = Thread.currentThread();
      int waitTime = (int)(Math.random() * 1000);
      try {
        me.sleep(waitTime);
synchronized(theCubbyHole){
        theCubbyHole.put(item);
      System.out.println("CubbyFiller put" + ", item = " + item);
      } catch (InterruptedException ie) {
}

Similar Messages

  • Under search in maps, names come up that aren't in my contacts. How does this happen?

    Under search history in the Maps app,...along with all the other places I
    did search for, some addresses comes up that I'm not familiar with, and they stay
    there. In some cases I didn't search for them at all. Some I may have. A name also
    appears, under the address, like "from John Doe". It’s a name that
    I'm familiar with my email, but is not in my contacts. Why and how does
    this happen?
    For example, a person I receive emails from for work, came up as the
    "from", and the address, after I searched for it (not on my phone).....I found out
    it was the address of his company, I place I've never searched for, nor is
    there a hyper-link to it any emails/websites that I could have clicked on
    accidentally.
    In another example, the beginning of an address “5165” which I may have searched
    for and starting typing in came up in the history with several locations that
    start with those numbers. Most of them say “from current location”, but one says from
    a name, whom again I’m familiar with by email, but is not in my contacts and
    have no idea of why the name would be somehow connected to the address.

    Thank you for the reply. I really appreciate it. Do you know why the names are attached, as "from"?
    I attached a screen shot image. "Harry" and "Angela" are not contacts in my phone. Who would their names get there?

  • My iPhone was stolen and I have contacted the police who are using the meid number to locate.  How does this work and what are my chances of getting the phone back?

    My iPhone was stolen.  I used Find My iPhone app to lock it and display a message.  The phone has not connected to the internet to locate it.  I contacted the police and they have taken my meid number.  How does this work and what are my chances of getting the phone back?  Are there other ways the theif can use it.  I was told once they put in a new sim card and use it, whatever software the police have, it will show up.

    Honestly? In the US (I can't speak to other countries, though I doubt it works much differently in a lot of the world) The police took your report and filed it either in their computers or, on paper. They will now not think of this again. The only time it will cross anyones mind is if, in the course of entering information into evidence about items recovered or seized at a crime scene, the serial number of an iPhone that was found/seized happens to match yours, in which case you will be contacted.
    The police in the US can and will do nothing to 'blok' the phone and it's not worth their time to try and locate it unless you know for a fact that it was stolen by a big time drug lord, master criminal, or some other such prime target and they can get a court order to track the location of the phone in order to locate this individual for your own purposes.  If they do that, they'll probably keep him under surveilance for a year or so before they act.
    Basically, the police don't care about your phone. If they find it, they will give it back to you. They are not, however, going to go looking for it. They have better things to do.
    I'm sorry, but that's the way it is.

  • Was told a USB device would create a PDF automatically from the data contained within when plugged into my USB port. Doesn't seem to work. How does this work and how can I fix the device I received?

    Acrobat is not on the USB device - only Adobe Reader needed on the PC. How does this work? How can I fix this device if at all?

    You need to ask the people who sold it to you. Doesn't sound like it has anything to do with Reader.
    However, you should be wary of devices that automatically try to open files when plugged in. They can also infect your computers with all kinds of viruses or malware.

  • HT2357 So how does this work on Mountain Lion? I cannot seem to ignore the iTunes 11 updates, which now seem to appear every 5 minutes!!

    So how does this work on Mountain Lion? I cannot seem to ignore the iTunes 11 updates, which now seem to appear every 5 minutes!!

    Fantastic!  Didn't work at first so restarted App store and tried again.  This time it asked 'ignore update'.  All gone!

  • How does this work with multiple systems backing up with time machine

    I was wondering as Apple states that this will do multiple time machine backups as in multiple computers, how does this do this? I am assumng that it will require me to partition the srive in the unit as multiple partitions. 1 for each time computer with time machine running.

    I'm sorry, I'm not quite sure how to interpret your answer so I'll ask for clarification.
    Do you mean that the backup belonging to each user will be protected by filesystem perms? In other words Alice's backup will be owned by Alice and only readable by her, ditto for the hyperthetical Bill?
    I guess in other words, does Time Machine preserve file ownership perms?

  • TS1425 My co worker has given me his iPod to take home and transfer his music to my laptop, however, I am unable to access his music to download it to my computer.  How does this work?

    My co-worker has given me his iPod to take home and transfer his music to my laptop, however, I am unable to access his music to download it to my computer.  How does this work?

    You need to transfer the iTunes Library from the most recent backup you made before the hard drive was replaced.
    You can't transfer the full iTunes Library from the iPad back to iTunes.
    There are third-party Windows applications that will transfer data from an iOS device, but they don't re-create the iTunes Library exactly as it was before.

  • I was using my notes earlier today and now for some reason they are all gone. How does this happen and is there anything I can do to get them back, the new ones also that haven't been synced.

    I was using my notes earlier today and now for some reason they are all gone. How does this happen and is there anything I can do to get them back, the new ones also that haven't been synced.

    See Recover your iTunes library from your iPod or iOS device.
    tt2

  • How does this IMPORT statement works

    can any one tell me how does this statement will work..
    I am wokring on Solution manager system , where in there is a function module SSF_FIELD_LIST
    to which system passes form name.
      import fields to fieldlist
             from database stxfcontr(sf) id l_fullname.
    stxfcontr is a table which contains value of in  a diff
    Regards,
    mayank

    It will import data object stored in memory under named fields to your custom data object fieldlist .
    The table which it is stored is name stxfcontr under RELID (memory area) SF .
    Cluster key (key of the entry) in that table is behind var l_fullname
    Anyhow I think [this link|http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3bf8358411d1829f0000e829fbfe/frameset.htm] is more that any explanation. Once you study it, you will understand the above.
    Regards
    Marcin

  • How does this happen: The picture of recent apps you get when double clicking the home button showed in Settings-Wifi that I had been connected to a network I have not been near for two months. Its not a recent "image" of Settings-Wifi. Can anyone explain

    How does this happen: The picture of recent apps you get when double clicking the home button showed in Settings-Wifi that I had been connected to a network I have not been near for two months. Its not a recent "image" of Settings-Wifi. Can anyone explain?

    Greetings,
    I've never seen this issue, and I handle many iPads, of all versions. WiFi issues are generally local to the WiFi router - they are not all of the same quality, range, immunity to interference, etc. You have distance, building construction, and the biggie - interference.
    At home, I use Apple routers, and have no issues with any of my WiFi enabled devices, computers, mobile devices, etc - even the lowly PeeCees. I have locations where I have Juniper Networks, as well as Aruba, and a few Netgears - all of them work as they should.
    The cheaper routers, Linksys, D-Link, Seimens home units, and many other no name devices have caused issues of various kinds, and even connectivity.
    I have no idea what Starbucks uses, but I always have a good connection, and I go there nearly every morning and get some work done, as well as play.
    You could try changing channels, 2.4 to 5 Gigs, changing locations of the router. I have had to do all of these at one time or another over the many years that I have been a Network Engineer.
    Good Luck - Cheers,
    M.

  • In Mac Mail on OS 10.5.8 I am receiving bogus emails which claim to be sent by people in my address book, but actually are not.  How does this happen and how can I correct this problem

    n Mac Mail on OS 10.5.8 I am receiving bogus emails which claim to be sent by people in my address book, but actually are not.  How does this happen and how can I correct this problem

    You said:
    I am receiving bogus emails which claim to be sent by people in my address book, but actually are not.
    ...and:
    Are you saying that my address book has not been hacked into?  That others are getting these email addresses from another source?
    This confuses me.  Are you saying that you are receiving bogus e-mails from some of your contacts, or are you saying that they are receiving bogus e-mails from you?
    If the latter, there are a number of reasons that people might be getting e-mail from you.  Malware, though technically possible, is extremely unlikely.  See Someone is sending messages from my e-mail address!
    If the former, that's rather unusual.  The only decent explanation I can think of is that a bunch of your Windows-using contacts got infected with something and their machines are being used to spam everyone in their contact lists, which would include you.

  • How does this make any sense??

    My husband gets the three bureao FICO subscription....this is on his credit report under "negative factors" HOW does this make ANY sense?!? It has "too many credit accounts with balances" as a negative factor...he has one credit card.... You have too many credit accounts with balances.EquifaxThe FICO® Score considers the number of accounts on a credit report showing a balance.Generally speaking, carrying fewer accounts with balances is considered less risky.For credit cards , your credit report may still show a balance on those cards - even if they are paid off in full each month. The total balance on the last statement is generally the amount that is shown on a credit report. NUMBER OF YOUR ACCOUNTS CARRYING A BALANCE Equifax1 accountFICO High Achievers have an average of 3 credit accounts carrying a balance.

    I think they're trying to say that he doesn't have enough accounts and with only one reporting (with a balance of course), that's their common answer.

  • HT3702 I reported a problem and I got a confirmation saying that my money was refund, I have't received anything yet. How does this work?

    I  reported a problem and I got a confirmation saying that my money was refund, I have't received anything yet. How does this work?

    It should be added either to your iTunes account, or possibly to your credit card if the purchase was made on with it.
    How long ago did you get the email ? It may take 48 hours for it to appear - if it's been longer than that and you can't see it on your iTunes balance (and you've tried logging out and back into the account to see if the balance updates) nor on your credit card then try contacting Support again, and include your case number if you have one.

  • Now what do I do with this?  How does this solve my problem?

    Now what do I do with this?  How does this solve my problem with Adobe Photoshop Elements 10

    And exactly what? You need to be a lot more specific.
    Mylenium

  • My number has been showing up as calling people while in my possession and sometimes off.  How does this happen?  How do I stop it?

    My number has been showing up as calling people while in my possession and sometimes off.  How does this happen?  How do I stop it?
    I've tried to contact facebook support and been trying to get on live chat for hours to no avail.  Please help me stop annoying people.

    A month ago I received a spoofed call at 6A - obviously I was not happy. Came from 000-0000000 as I recall.
    Tonight I had two irate people call me minutes apart saying I called them - I was eating dinner with my wife with my phone turned off in our eye-sight.
    I cannot believe that we allow spoofing software and spoof cards these days, with only a crime if someone asks for money or does some similar fraudulent activity.

Maybe you are looking for

  • Loading multi MC's into SP

    Can I use contentPath to attach multiple instances of a movieclip? At the moment, I can attach without using contentPath but this does not help me at all. If I cannot use contentPath the scrollPane is useless - meaning none of the features of the scr

  • "Must enter batch name"...***?

    Oh, Compressor, you lame-o app... OK...I've never seen this one before, in all the years I've used the various versions of Compressor. It's asking me to enter a "Batch Name". Isn't that the name of the "batch" I just saved, with a name? And, as long

  • Facebook albums disappear?

    I have 14 photo albums on Facebook, all uploaded from Aperture 3. They are still there, in my FB profile, but they DISAPPEARED from my Aperture 3 Library. My God, where did they go? This software is really unpredictable (and unreliable?). I am pretty

  • Multiprovider Modelling

    hi,   I have one multiprovider with 8 cubes. I found some of the cubes are not efficient and i re-arranged the dimensions. Now in 2 cubes i got 3 more dimensions. Now do i need to change the multiprovider dimensions also and include these 3 new dimen

  • HELP! Where is Dashcode?

    Where is dashcode? I tried to find it in Spotlight, but it didn't find it. I searched in Finder, and I still can't find it. Could someone please tell me where it is?