How to solve this race condition

Hi there,
the following code was taken from an official Java Tutorial at http://java.sun.com/docs/books/tutorial/essential/threads/synchronization.html but it's bugy 'cause it's creating race conditions I cannot handle.
// this is the consumer
// it tries to get the content of cubbyhole
// but as soon as "available" becomes "false"
// the content can change
public synchronized int get() {
        while (available == false) {
            try {
                wait();
            } catch (InterruptedException e) { }
        available = false; // <-- the producer can access the cubbyhole
        notifyAll(); // the producer is being notified (wake up)
        return contents;
    }Imagine the scheduler interrupts the consumer just after the "notifyAll()" method (race condition). The producer can access the cubbyhole (available = false) and store a new value in it. Due to the fact that the cubbyhole is a reference the "return contents" command in the consumer will now return the new (overwritten) value. This code definitely won't work. The only way it works is to clone (!) the current "contents" in a separate variable BEFORE awaking the producer and enabling it to make changes.
I had serious troubles in using the wait() methods and like to know your oppinions/experiences about that.
Thx for reading.
90210

I'm not going to bother looking at that tutorial, but I'd venture to guess that you are making a false assumption. Since get() is synchronized, no other thread can acquire a lock to the object being synchronized on while this method is changing the value of 'available' to false. But without seeing the context of what those other threads might be doing, it's hard to tell if you really have a valid claim or not (that it's 'buggy'). But 99.99999% of the time, its your own mistake/misinformation/invalid analysis.

Similar Messages

  • I recieve SMS and i hear established melody and I do't hear the person on all over SMS. How to solve this problem?

    Problem with an incoming message. For example, during my conversation the second line receives a call, I hear the sound in dynamics such as "piiiip piiiip," but when in this situation I recieve SMS and i hear established melody and I do't hear the person on all over SMS. How to solve this problem? Perhaps someone tell me? save in advance

    Not Charge
    - See:     
    iPod touch: Hardware troubleshooting
    iPhone and iPod touch: Charging the battery
    - Try another cable. The cable for 5G iPod (lightning connector) seems to be more prone to failure than the older cable.
    - 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'm trying to connect my 30" Cinema Display to my new Mac Pro along with a new Apple 23'" monitor.  The new Apple monitor is fine.  On the cinema monitor everything is blown up pretty large.  Any ideas on how to solve this?

    I'm trying to connect my 30" Cinema Display to my new Mac Pro along with a new Apple 23'" monitor.  The new Apple monitor is fine.  On the cinema monitor everything is blown up pretty large.  Any ideas on how to solve this?

    The 30" display needs a DUAL-LINK adapter.
    <http://store.apple.com/us/product/MB571Z/A/mini-displayport-to-dual-link-dvi-ada pter>

  • Hi, when ever I'm using 3G, on my Iphone4 sim stops working and Network is lost, this started after I updated my phone with  6.0.1(10A523)version. Please help how to solve this problem.

    Hi, when ever I'm using 3G, on my Iphone4 sim stops working and network is lost, this started after I updated my phone with  6.0.1(10A523)version. Please help how to solve this problem. Thanks.

    Photos/videos in the Camera Roll are not synced. Photos/videos in the Camera Roll are not touched with the iTunes sync process. Photos/videos in the Camera Roll can be imported by your computer which is not handled by iTunes. Most importing software includes an option to delete the photos/videos from the Camera Roll after the import process is complete. If is my understanding that some Windows import software supports importing photos from the Camera Roll, but not videos. Regardless, the import software should not delete the photos/videos from the Camera Roll unless you set the app to do so.
    Photos/videos in the Camera Roll are included with your iPhone's backup. If you synced your iPhone with iTunes before the videos on the Camera Roll went missing and you haven't synced your iPhone with iTunes since they went missing, you can try restoring the iPhone with iTunes from the iPhone's backup. Don't sync the iPhone with iTunes again and decline the prompt to update the iPhone's backup after selecting Restore.

  • I Try to open an Indesign document. The message: it is made in a newer version. Go tot CC: Help/Give your Adobe id/Start Indesign again and try to open the document. This doesn't work. How to solve this problem?

    I Try to open an Indesign document. The message: it is made in a newer version. Go tot CC: Help/Give your Adobe id/Start Indesign again and try to open the document. This doesn’t work. How to solve this problem?

    What version are you running?
    What version was it made with?

  • After installing Lion os I cannot see my desktop 1 nor can I see my open windows when I go to finder, I just get the grey/black background that comes with mission control. Any ideas as to how to solve this problem?

    The top picture is what my Desktop 1 looks like. And the picture below is what my desktop looks like after clicking mission control. Ive tried changing wallpapers, changing preferences, etc. and I still can't figure out what's wrong. As you can see, on mission control you can't see any of the open windows, or anything that I have placed on my desktop. Any ideas as to how to solve this issue?

    Hello cor-el, thanks for your reply. I changed my settings for downloads to desktop and it has appeared on there. When I double click I am asked which program I want to open file. I click firefox and another box "opening install" says I have chosen to open the file which is an application and do I want to save it. This is the only real option so I press save file. I get a box saying this is an executable file which may contain viruses - do you want to run. I press ok and the final box showing C drive file name and desktop appears stating application not found.
    This happens the same whenever I try to install.
    To my untrained eye the application is not being recognised as an application and I cannot work out how to get it to do that.
    My plugin is still showing as out of date.
    Is there anything you could suggest. Thanks for your time.

  • Hello guys..!! I have got my new  iphone 4 2 days ago.The same day it got a problem of auto restart in every 1, to 2 minutes. I updated and restored it many times...but of no use... can any one help me how to solve this problem..!!

    Hello guys..!! I have got my new  iphone 4 2 days ago.The same day it got a problem of auto restart in every 1, to 2 minutes. I updated and restored it many times...but of no use... can any one help me how to solve this problem..!!

    Go to Settings/General/Reset - Erase all content and settings. the connecto to iTunes and restore as a New phone. Do not restore any backup. If the problem persists you have a hardware problem. Take it to Apple for exchange.
    This assumes that the phone is not hacked or jailbroken. If it is you will have to go elsewhere on the internet for help.

  • TS1702 Does any one know how to solve this iPod touch error: I am tryin to pay for an app but I can because it says that I don't have enough space when I actually have more than enough space to download it so please help me

    Does anyone know how to solve this problem: I a trying to download a paid app but I cannot because the AppStore says that info not have enough space to download it even though I have more than enough space so if any of you know what can solve this problem then please let me know

    The app is compressed and needs swap space to install. As a rule of thumb you need 3 times the listed size of the app available, or in your case 4.6GB. You only have 1.7GB.

  • Iphone 4 is hiting in ios 5.1.1. any one know how to solve this problem??

    iphone 4 is hiting in ios 5.1.1. any one know how to solve this problem??

    This sounds like a hardware problem and should be taken to Apple if the heating / "hiting" becomes disturbing to you. If you feel that your iPhone has a heating / "hiting" problem and needs to be repaired, please contact Apple Support and they can further assist you in having it inspected, repaired, or even replaced. In the past, the overheating of Apple products has been known to cause harmful problems to a few particular users. Please evaluate your situation and take further action if you feel necessary.
    http://www.apple.com/support/contact/

  • Transaction failed for unknown reason (100) Unable to complete backup at this time. Does anyone know how to solve this issue?

    Transaction failed for unknown reason (100) Unable to complete backup at this time. Does anyone know how to solve this issue?
    Thanks.

    The system is set up to backup files to the iCloud at the end of the day. This has ot happen for sometime now and the mesaage I get is the back up error.

  • How to solve this error java.util.MissingResourceException

    Hi Friends,
    I had developed one web dynpro application by using  Internationalization - I18N of WebDynPro (Java)  Application (Blog)   but I got one error that is
    <b>java.util.MissingResourceException: Can't find bundle for base name com.sap.example.language.lang, locale en_US</b> 
    Actually i created two properties file
    1. lang_en.properties
    2. lang_ta.properties
    I stored this two properties file in this package com.sap.example.language
    and this my code in DOInit()
    sessionLocale =  WDClientUser.getCurrentUser().getLocale();
    resourceHandler = ResourceBundle.getBundle("com.sap.example.language.lang",sessionLocale);
         catch (WDUMException e)
         e.printStackTrace();
         wdContext.currentContextElement().setUsername_label(resourceHandler.getString("testview.username"));
         wdContext.currentContextElement().setPassword_label(resourceHandler.getString("testview.password"));
    How to solve this error?
    Guide me.
    Advance Thanks,
    Balaji

    Hi Friends,
    I had developed one web dynpro application by using Internationalization - I18N of WebDynPro -Java Application (Blog) but I got one error that is
    <b>java.util.MissingResourceException: Can't find bundle for base name com.sap.example.language.lang, locale en_US</b>
    Actually i created two properties file
    1. lang_en.properties
    2. lang_ta.properties
    I stored this two properties file in this package com.sap.example.language
    and this my code in DOInit()
    sessionLocale = WDClientUser.getCurrentUser().getLocale();
    resourceHandler = ResourceBundle.getBundle("com.sap.example.language.lang",sessionLocale);
    catch (WDUMException e)
    e.printStackTrace();
    wdContext.currentContextElement().setUsername_label(resourceHandler.getString("testview.username"));
    wdContext.currentContextElement().setPassword_label(resourceHandler.getString("testview.password"));
    How to solve this error?
    Guide me.
    Advance Thanks,
    Balaji

  • Get error message when syching iPod, iPod cannot be synced. You do not have enough access priveleges for this operation.  How to solve this?

    Get error message when syching iPod, iPod cannot be synced. You do not have enough access priveleges for this operation.  How to solve this?

    Hello FranBNYC
    It sounds like a permissions issue with your library, check out the article below to check and make sure that things are set correctly.
    Trouble adding music to iTunes library or importing audio CD
    http://support.apple.com/kb/ts1387
    Thanks for using Apple Support Communities.
    Regards,
    -Norm G.

  • Hi, my Ipod Schuffle (4 gen) dissapears from windows vista right after I execute Itunes, I've change the drive letter, erase temps in App data file, verified USB drivers, etc... Can someone guide on how to solve this?

    Hi, my Ipod Schuffle (4 gen) dissapears from windows vista right after I execute Itunes, I've change the drive letter, erase temps in App data file, verified USB drivers, etc... Can someone guide on how to solve this?

    If you want the shuffle to be always visible in Windows (outside of iTunes), you need to Enable disk use.
    Select the shuffle in the iTunes sidebar, under DEVICES.  Over to the right, select the Summary tab.  You should see a checkbox that says Enable disk use.  Check it and click Apply.
    NOTE:  If disk use is enabled, you have to Eject the shuffle in iTunes, BEFORE you physically disconnect it.  If disk use is NOT enabled, you can disconnect the shuffle at any time, as long as iTunes is not doing something with it at that moment (such as syncing); the shuffle's light will then be blinking (to indicate DO NOT DISCONNECT).
    So, unless you want to use the shuffle like a USB flash drive (carry random files on it), you may want to NOT Enable disk use.

  • I'm using windows 8 on my pc, and i have just downloaded the latest version of itunes, after i upgraded the latest version of itunes, it can't detect my iphone, but my windows/my pc does. Anyone knows how to solve this problem?

    I'm using windows 8 on my pc, and i have just downloaded the latest version of itunes, after i upgraded the latest version of itunes, it can't detect my iphone, but my windows/my pc does. Anyone knows how to solve this problem?

    Hi jstutsman,
    If you are having issues with your iPod nano not being recognized in iTunes after a recent update, you may find the steps and links listed in the following article helpful:
    iPod not appearing in iTunes - Apple Support
    Regards,
    - Brenden

  • My external hard drive will no longer connect to my macpro......any ideas on how to solve this problem??

    My external hard drive will no longer connect to my macbook pro via FW800........Any ideas on how to solve this??

    Reset the SMC, see this Apple note: http://support.apple.com/kb/HT3964
    Also reset the PRAM:
    Shut down the computer.
    Locate the following keys on the keyboard: Command, Option, P, and R. You will need to hold these keys down simultaneously in step 4.
    Turn on the computer.
    Press and hold the Command-Option-P-R keys. You must press this key combination before the gray screen appears.
    Hold the keys down until the computer restarts and you hear the startup sound for the second time.
    Release the keys.
    In most cases, those two actions will fix the port issue. If it's still an issue, you may need to see if the external hard drive can connect to another system, to ensure it's not having problems.

Maybe you are looking for

  • Generate Multiple Invoices in One PDF

    Hi, I have requirement whereby I need to generate multiple invoices in one PDF file. I am using Oracle BI Publisher with Oracle APEX 4.0.2 If there is only one invoice that needs to be generated in one PDF file then I already have the solution whereb

  • Adffaces_skin.war download

    "An invalid character was found in text content. Error processing resource 'http://www.oracle.com/technology/products/jdev/101/howtos/adfskins/adffaces-skin.war" The above link from the "Developing and Using ADF Faces Skins" HowTo tutorial results in

  • Decrypt Errors occuring in WLC Log

    Hi all, we see a strange message in our WLC logs, which occurs quite often (>10 times a day): Decrypt errors occurred for client [MAC-Adress] using WPA key on 802.11b/g interface of AP [MAC-Adress] The MAC-Adresses of the affected clients are varying

  • Straightening and Crop tool not working.  What to do?

    I've been working with photos all morning, and now neither my Straightening Tool or Crop Tool are working.  What to do?

  • How to Upload XSD into MDS by using WLST/ANT only

    Hi, Can anyone help me out how to upload an XSD into MDS using WLST/ANT only (Not JDEV) Please help me out its urgent Edited by: 943121 on Dec 28, 2012 8:11 AM