How static and nonstatic method synchronizes if both of them are using same

class ThreadA
     public static void main(String args[])
          ThreadB b=new ThreadB();
          b.setName("MyThread");
          b.start();
     synchronized(b){
          try{
               System.out.println(" wating for b to complete...");
               b.wait();     
          catch(InterruptedException e)
          System.out.println(b.total);
class ThreadB extends Thread
     int total;     
     public void run()
     System.out.println(Thread.currentThread().getName());
     synchronized (this)
          for(int i=0;i<10;i++)
               total+=i;
     notify();
in this example both the threads : ThreadB and main thread are trying to use sameobject i.e b
then when ThreadB will get started and immediately synchronize b i.e get a lock on b then how main will get
the lock and again synchronize b. How the main thread and ThreadB will run simultaneously ...please clear this .
the output of this program is :-
waiting for b to complete
45

I reckon the main thread is getting the lock first. If it weren't it would never finish. The println at the start of the ThreadB probably delays the thread enough to enable the main thread to win the race mostly. So the main thread gets the lock and then it goes into wait(), which releases the lock and b does it's loop, calls notify() and finishes, releasing the lock. Now the main thread, having been released from wait takes the lock back, then winds up.
What's wrong with this code is that if b got the lock first it would call notify() before the main thread called wait(), which would do nothing and and main thread would stick on wait indefinitely.
Wait and notify should always be used in conjuction with some kind of flag which says whether the thread needs to wait or is the condition to continue already met.

Similar Messages

  • Name space conflict between static and instance method

    Hello,
    there seems to be a very unfortunate name space conflict between static and instance method name. Consider:
    static String description() that should return a description of a class.
    String description() that should return a description of an instance.
    That seems to confuse the compiler. How come? Static and instance methods don't have anything to get confused about.
    Thanks for any insights :-)

    Umm...jeez.
    It's not a bug, it's the way it's supposed to be.
    Since a static method can be called the same way an instance method
    A instance = new A();
    A.staticMethod();
    instance.staticMethod();it's not allowed.
    Also in the class, you can call
    public void myMethod() {
          instanceMethodInClass();        // You don't need this, it's superfluous
          staticMethodInClass();          // You don't need the class name.
    }If you didn't understand, then just accept the fact that it is so. Some day you'll understand it.

  • If i have already logged in my iTunes with my iPod and plugged that in, but now i have a new iPod and I want to plug that in to my laptop how would i do that with out losing anything on my new iPod and my old one. Both my ipods are on different accounts?

    If i have already logged in my iTunes with my iPod and plugged that in, but now i have a new iPod and I want to plug that in to my laptop how would i do that with out losing anything on my new iPod and my old one. Both my ipods are on different accounts?

    Nothing special. See:
    How to Set Up Two iTunes Accounts on One Computer | Chron.com
    iTunes: How to share music between different accounts on a single computer

  • HT4972 When I take photos of myself, the image come out backwards.  I have an iPad2 and an iPad 3, and both of them do the same with my self portraits.  Any ideas on how to remedy this?

    When I take photos of myself, the image come out backwards.  I have an iPad2 and an iPad 3, and both of them do the same with my self portraits.  Any ideas on how to fix this.  I use wifi.

    Gmabert-
    The front camera displays a reversed image on the screen, as if you were looking into a mirror.  When you take a picture, it comes out normal, which is backwards to what you would have observed on the screen.
    Fred

  • How do i change the administrator name and password when i forgot both of them

    how do i change the administrator name and password when i forgot both of them?

    Find the user name and then reset the password.
    Start the computer,then press and hold down command and R keys to start into recovery partition.
    When you see the Apple logo, release the keys.
    Wait until  OS X Utilities window shows up.
    Move the mouse to the menubar at the top and click "Utilities", then select "Terminal"
    from the drop down.
    Terminal window will appear.
    Type in   resetpassword   and press enter on the keyboard.
    Leave the Terminal window open.
    Reset Password Utility window will open with Macintosh HD selected.
    Select the user account from the popup menu box under “Select user account”.
    Note down the user account name.
    Enter a new password.
    Reenter the new password for the user.
    Note it down.
    Enter a hint.
    Click the "Save" button.
    Click  in the menubar and select Restart.
    Log in.
    If Keychain dialog box appears, select “Create New Keychain”.

  • How do I use my wireless Led TV as a second monitor using my home network without any cables? Both of them are connected to home network

    How do I use my wireless Led TV as a second monitor to my mac book pro using my home network without any cables? Both of them are connected to home network

    Having a gateway there is not going to be a problem. We just need to configure the Time Capsule correctly to work with the 2Wire.
    You will need one ethernet cable to connect the 2Wire to the Time Capsule and a second ethernet cable to temporarily connect your computer to the Time Capsule so that it can be configured.
    The 2Wire you have now should already be broadcasting a wireless network, so once you have the Time Capsule setup, you will actually have two wireless networks there.
    Here's how to configure the Time Capsule to work with the 2Wire:
    Connect an ethernet cable from one of the LAN <-> ports on the 2Wire to the WAN (circle icon) port on the Time Capsule.
    Connect another ethernet cable temporarily from your computer to one of the LAN <-> ports on the Time Capsule
    Open Hard Drive > Applications > Utilities > AirPort Utility
    Click Manual Setup
    Click the Base Station tab below the row of icons to assign a name for the Time Capsule, a device password (write this down) and adjust time zone settings
    Click the Wireless tab below the row of icons
    Wireless Mode....Create a wireless network
    Wireless Network Name....Your choice
    Radio Mode....Automatic
    Channel...Automatic
    Wireless Security...WPA/WPA2 Personal
    Wireless Password....Your choice (write this down)
    Check mark next to....Remember password in keychain
    Click the Internet icon
    At the bottom, change the setting for Connection Sharing to "Off (Bridge Mode)"
    Click Update at the lower right and the Time Capsule will restart after 20-25 seconds and you should get the green light.
    You should now be able to connect to your Time Capsule wireless network.
    I'm not clear on whether you have tried to setup the Time Capsule to backup your computer. Post back if you need some info on that. We may need to start another thread for that as that will be a different subject.

  • How can I file share with another person if both of us are using Mac operating systems?  Do we need to use a third party file sharing system or does apple have this capability?

    How can I file share with another personif both of us are using Mac operating systems (one of us using a Mac laptop and the other using iMac).  Our intention is to have a working document that can be changed by both parties over time and both parties will have visibility to the others changes.

    Use SugarSync

  • Hi all! I need to buy iPad tomorrow. Need your advice which iPad to buy? For almost the same money I can buy iPad 2   3g or iPad 4 wireless only. Both of them are 16gb and price difference is 70€ for iPad 4

    Hi all! I need to buy iPad tomorrow. Need your advice which iPad to buy? For almost the same money I can buy iPad 2 + 3g or iPad 4 wireless only. Both of them are 16gb and price difference is 70€ for iPad 4

    16GB vs 32GB vs 64GB: Which new iPad storage capacity should you get?
    http://www.imore.com/2012/03/08/16gb-32gb-64gb-ipad-capacity/
    How much content will fit on my iPod or iPhone?
    http://support.apple.com/kb/HT1867
    iPad Mini vs. iPad 2 vs. iPad 4: Which iPad Should You Buy?
    http://www.padgadget.com/2012/10/25/ipad-mini-vs-ipad-2-vs-ipad-4-which-ipad-sho uld-you-buy/
     Cheers, Tom

  • HT4914 My wife and I both have iPhones and iPads. Can we both have access to the same iTunes Match library or do we have to have separate subscriptions?  Our devices are synced to the same computer and we currently download titles from the same library.

    My wife and I both have iPhones and iPads. Can we both have access to the same iTunes Match library or do we have to have separate subscriptions?  Our devices are synced to the same computer and we currently download titles from the same library.  We both use separate apple id's on our devices.

    You need to reconfigure your phone for use with your Apple ID. I suggest you restore yours as new then reconfigure it with your information and Apple ID. Be sure to disable Find My Phone, if it's enabled, before proceeding.
    Locked Out, Forgot Lock or Restrictions Passcode, or Need to Restore Your Device: Several Alternative Solutions
    A
    1. iOS- Forgotten passcode or device disabled after entering wrong passcode
    2. iPhone, iPad, iPod touch: Wrong passcode results in red disabled screen
    3. Restoring iPod touch after forgotten passcode
    4. What to Do If You've Forgotten Your iPhone's Passcode
    5. iOS- Understanding passcodes
    6. iTunes 10 for Mac- Update and restore software on iPod, iPhone, or iPad
    7. iOS - Unable to update or restore
    Forgotten Restrictions Passcode Help
                iPad,iPod,iPod Touch Recovery Mode
    You will need to restore your device as New to remove a Restrictions passcode. Go through the normal process to restore your device, but when you see the options to restore as New or from a backup, be sure to choose New.
    You can restore from a backup if you have one from BEFORE you set the restrictions passcode.
    Also, see iTunes- Restoring iOS software.

  • I have an Apple TV . My husband has an I-pad 2  and I Have an I-Pad 3. When my I-Pad 3 is used we have no problems. However when we try to use my husbands I-Pad 2 video mirrioing lags behind  for minutes. We checked settings and both I-pads are the same.

    I have an Apple TV . My husband has an I-pad 2  and I Have an I-Pad 3. When my I-Pad 3 is used we have no problems. However when we try to use my husbands I-Pad 2 video mirrioing lags behind  for minutes. We checked settings and both I-pads are the same. What can one do to use the I-Pad 2 without any mirroring lag. Thank you All in advance.

    try restarting
    both ipad http://support.apple.com/kb/ht1430
    and atv
    restarting ios devices cure 80% of all troubles users have

  • HT1386 we are using same laptop with my wife for our 2 iphones and when sincronizing, she's getting my apps and is requested for my apple id and password when getting a new app on her device. How can i fix that issue?

    we are using same laptop with my wife for our 2 iphones and when sincronizing, she's getting my apps and is requested for my apple id and password when getting a new app on her device. How can i fix that issue?

    Each phone needs to set its own sync settings on the various tabs in iTunes.  On the Apps tab, for example, uncheck the apps that you don't want on the phone being synced.  This article may also be of interest: http://gigaom.com/apple/itunes-101-multiple-devices-one-itunes-account/.
    You can change the Apple ID used for purchasing on her phone by going to Settings>Store>Apple ID, tap the ID shown, sign out, sign back in using her Apple ID.  However, be aware that if she has any apps on her phone purchased with your Apple ID she will still be prompted to enter the password associated with this ID for all future updates of these apps.  This is because apps are permanently tied to the Apple ID used to purchase them, regardless of the ID in Settings>Store.

  • I just bought my first iphone 4 actually i bought two and for some reason on both phones one of the speakers does not work im just wondering if this is normal because i took it back to the store and they told me all of the phones are the same?????

    i just bought my first iphone 4 actually i bought two and for some reason on both phones one of the speakers does not work im just wondering if this is normal because i took it back to the store and they told me all of the phones are the same?????

    There is only one speaker, what looks like another speaker is actually a microphone. All iPhones only have one speaker down by the dock connector.

  • Fire fow will not let me use curtain buttons and drop down menus for web sites. i had version 7.01 and upgraded to verson 8.02b and it did it on both of them. and the web pages work find if i use chrome

    Firefox will not let me use curtain buttons and drop down menus for web sites. i had version 7.01 and upgraded to verson 8.02b and it did it on both of them. and the web pages work find if i use chrome

    Do a clean reinstall and delete the Firefox program folder before (re)installing a fresh copy of the current Firefox release.
    Download a fresh Firefox copy and save the file to the desktop.
    *Firefox 21.0.x: http://www.mozilla.org/en-US/firefox/all.html
    Uninstall your current Firefox version, if possible, to cleanup the Windows registry and settings in security software.
    *Do NOT remove personal data when you uninstall your current Firefox version, because all profile folders will be removed and you will lose all personal data like bookmarks and passwords from profiles of other Firefox versions.
    Remove the Firefox program folder before installing that newly downloaded copy of the Firefox installer.
    *(32 bit Windows) "C:\Program Files\Mozilla Firefox\"
    *(64 bit Windows) "C:\Program Files (x86)\Mozilla Firefox\"
    *It is important to delete the Firefox program folder to remove all the files and make sure that there are no problems with files that were leftover after uninstalling.
    *http://kb.mozillazine.org/Uninstalling_Firefox
    Your bookmarks and other personal data are stored in the Firefox profile folder and won't be affected by an uninstall and (re)install, but make sure that "remove personal data" is NOT selected when you uninstall Firefox.
    If you keep having problems then also create a new profile.
    *http://kb.mozillazine.org/Profile_folder_-_Firefox
    *http://kb.mozillazine.org/Profile_backup
    *http://kb.mozillazine.org/Standard_diagnostic_-_Firefox#Clean_reinstall

  • Photoshop CS4- Does anyone know how to tell if two people are using same file?

    Photoshop CS4- Does anyone know how to tell if two people are using same file?

    Hi,
    Thanks for your response. I'm not the solution in the link.
    The problem that I am having at work it that several people in our department want to be able to update a file, but we want to be sure that no one else is currently editing that file. If someone has the file open, it's helpful to know the user's name so we can talk about when the next person can edit the file. This works in Excel files that we use, and I would love to find out how to do it in Photoshop and Illustrator.
    Thanks for the help!

  • We have a PCI-6110S to run our VIs. But, we are planing to buy another card (PCI-6024E​) to run real time PID vi. So, if they are installed at the same computer, would it be a problem for LabVIEW to run both of them at the same time.

    We have PCI-6110 S series installed in our computer. We are planing to purchase PCI-6024 E series card. They will be both installed at the same computer.
    1) I am wondering if we run both of them at the same time, would we have any problem?
    2) To run PID real time, can any E series of PCI card be used?
    Thanks...

    Hello Baho,
    1) Using Traditional NI-DAQ, there could be problems with the fact that the NI-DAQ driver is not multi-threaded; meaning that while waiting for one operation to complete, the driver could cause another operation to time out. On the other hand, DAQmx is multi-threaded, and would eliminate this problem. At the time of this writing though, DAQmx does not yet support simultaneous-sampling boards like the PCI-6110S. A good solution then would be to use Traditional DAQ for the 6110S and DAQmx for the E-series board, and they would by default run in separate threads.
    2) No, using a PCI E-Series board, the LabVIEW VI would have to run in a Windows OS and would not be Real Time or run deterministically. You can run the PID program in Windows, but the
    results could be inconsistent. The advantage of a Real Time is that the OS is deterministic or that the OS allows you to tightly control or determine the maximum time any function takes to operate. This kind of control of the maximum operation time is not possible with non-Real Time OSs such as Windows.
    If you would like to get set up with a Real Time System, I suggest you contact your Technical Sales Representative.
    Regards,
    Chad Evans
    National Instruments

Maybe you are looking for

  • Tie Machine  no longer backs up after lion install

    After Lion install, Time Machine no longer backs up-will start to back up but freezes after a few kb's-spinning clock will spin 24/7 but backup is stalled

  • Unable to create OSB project in OEPE 11.1.1.6.1

    Hi, I have installed weblogic server 10.3.4 with OEPE 11.1.1.6.1 (wls1034_oepe111161_win32.exe) on Windows XP machine. There after I installed Oracle Service Bus 11.1.1.4.0 (ofm_osb_generic_11.1.1.4.0_disk1_1of1.zip). When I open OEPE I do not find t

  • Can not login into my AppleID

    Hello. I cannot login into my Apple ID. I have never set up a second email nor I can remember the answers I put up for security questions. I do know my birth date though. Any other way I can restore my password or reset it?

  • InDesign CS4 Hyperlinks palette is blank

    I want to add hyperlinks to an InDesign CS4 document. I selected some text, opened the hyperlinks palette, and ... there are no UI elements showing on the palette. It's just completly empty or blank. Yet the instructions tell me to click on the link

  • Import map file

    Hi Experts, I can see that form MDM Console the Ports table under Admin node has field with type "Inbound" and the name of the import map.  Could anybody tell how to get this Import map file from the repository if my remote system is set up "Inbound/