InvokeLater() and invokeAndWait(): concerns over synchronization

I've been reading up on the methods invokeLater(Runnable) and invokeAndWait(Runnable) and I've understood this much: the difference is in the synchronization of the new thread with the main (or current) one. I somewhat familiar with what synchronization means (i.e. it's a way of guaranteeing that changes made to shared resources are seen by all threads as soon as they are made).
My question is: does a call to invokeAndWait() mean that I don't have to worry about synchronizing methods and code?

gib65 wrote:
My question is: does a call to invokeAndWait() mean that I don't have to worry about synchronizing methods and code?No. Both the invoke methods just push off code to be executed in the event thread. That might mean that you don't need synchronization, but only if the synchronization was needed because you had the event thread and another thread accessing same resources.
The difference is that invokeLater doesn't stop the execution of the current thread, while invokeAndWait stops the current thread until the task has been run on the event thread.

Similar Messages

  • Help on invokelater and invokeandwait

    Hello,
    i am developing an application using swing API.
    in this application i have to read ceratin data from the database. and do operation on the data.
    what i want to do is to fetch data based on a query and display the data to the user in a JTable on a separate JFrame. the user selects the row from the table, he wants to process and presses the select button on this frame, to set the static variable (say ROW_ID) in the class whcih invoked this frame.
    now what should happen is that the invoking class object should wait untill i press the select button (ie when the ROW_ID is set).
    i would like to know how to do it.
    please help
    thank you
    Message was edited by:
    vaibhavpingle

    now what should happen is that the invoking class object should wait untill i press the select button (ie when the ROW_ID is set).
    It doesn't sound like you want invokeLater() or invokeAndWait().
    I think you want one of two things: the more likely is that you want to be using a modal JDialog to allow the user to select a row (if you display the table, make the selection and then close that new window). The other possibility is that you want your original object to have state, which is modified (on the Event Dispatch Thread) when the user makes the row selection. Until that state is modified, certain functionality is disabled.
    i also tried to use the invokeAndWait(runnable) method but i got an exception saying that the call cannot be made
    What exception? I don't know of one which says "the call cannot be made."

  • InvokeLater() and invokeAndWait()

    I'm somewhat confused about when to use these.
    from http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html - it says "you must take extreme care that Swing components and models are created, modified, and queried only from the event-dispatching thread"
    I also understand that these two methods are basically the same though invokeAndWait only returns once the run() has completed while invokeLater will return immediately.
    So, when should I use these? and which one should I use?
    If I'm creating the GUI initially, use the invokeAndWait()
    If I'm updating/quering the GUI components, use invokeLater()
    is this the correct way of thinking?

    You only need to use these methods if you are updating your GUI from a Thread you have spawned. These methods insure the GUI udpate is performed from the event dispatcher thread as those methods put the GUI update in the event queue.
    If you are already updating the GUI from the event dispatcher thread (i.e. from an ActionListener, MouseListener, etc) you don't need to use them.
    For instance lets say you do an expensive database query (takes a noticeable amount of time to return results) when you press a button. If you just do the database query in the main thread the rest of your application will block while the db query is performed. So you would spawn a new thread with your db query in it. However, now lets say when the db query is over you want to update some status label to notify the user. You would put the label update in a Runnable object and pass that to invokeLater() which would put your runnable oject in the event queue which would eventually get executed by the event dispatcher thread.
    Now the rest of your application is responsive while the db query is occuring. And the label will be updated in a thread safe way when the db query is done.
    Generally invokeLater() is used, as invokeAndWait() blocks until the runnable object you add to the event queue is executed. This isn't generally desireable. More or less you can safely ignore invokeAndWait(), although keep it in mind for those rare occasions where its usage would be desireable.
    Here a very generic example:
    public void actionPerformed(ActionEvent e) {
          //Button has been pressed lets query the db in a thread
          new Thread() {
                public void run() {
                    //Hit the DB
                    Runnable changeLabel = new Runnable() {
                        public void run() {
                            label.setText("tell user db query is done");
                    //Put the changing of the label in the event queue
                    EventQueue.invokeLater(changeLabel);
            }.start();
    }

  • I Have a Security Concern Over the AT&T U-verse Router Pace Plc 5031NV-030

    Hello ?? I Have a Security Concern Over the AT&T U-verse Router Pace Plc 5031NV-030 Pace Plc 5031NV-030 Ser # 48131N052034 Hardware Version 2701-000875-004 Software Version 9.8.1.489233-att All someone Needs is a Ethernet cord in his back pocket. Connected to Your Router and They Can Find My "Secure" Network Wireless Key is Visable Just By Typing the Default http://192.168.1.254/ IP for my Gateway, in to a browser device on a portable device, so They Can Next Access My Network without My Permmission Net Time !!!!  This is a Serious Security Concern  on  A&TT's Part !!! AT&T Should Have a Concern Over This Too, I Do All I Can to Prevent Theft of My Intenet Service, But If I have a Guest Visit Me, And I Leave the Room Where This Router is Located, I Will Not Know If This Has Taken Place !!!??   AT&T Should Be Very Concerned in This Regard !!!!

     No, you should be concerned about the friends you have at your place.  If you think they will do that, I'd suggest you get some more trustworthy friends.
    Take some personal responsibility to protect your internet service.
    Personally I only use a wired ethernet connected laptop and have wireless turned off, so that concern is mitigated.
    Chris
    Please NO SD stretch-o-vision or 480 SD HD Channels
    Need Help? PM ATT Uverse Care (all service problems)
    ATT Customer Care(all other problems)
    Your Results May Vary, In My Humble Opinion
    I Call It Like I See It, Simply a U-verse user, nothing more

  • Hi there has been someone on my game clash of clans on my iPad and have spent over £50 and I need the money back what can I do?

    Hi there has been someone on my game clash of clans on my iPad and have spent over £50 and I need the money back what can I do?

    Hello Vaidas Vaidas,
    It sounds like you are noticing someone else is accessing your Clash of Clans data by playing the game and you have tried to reset your Apple ID password. If you are following the steps outlined in this article:
    Apple ID: Changing your password
    http://support.apple.com/kb/ht5624
    What is preventing you from changing your password? Any error messages or prompts?
    Thank you for using Apple Support Communities.
    All the best,
    Sterling

  • My itunes will not work at all. i have deleted and reinstalled in over ten times, i have ended the process, i have reinstalled quicktime i few times, i have deleted many folders. What else can i do? Please help:(

    My itunes will not work at all. i have deleted and reinstalled in over ten times, i have ended the process, i have reinstalled quicktime i few times, i have deleted many folders such as TEMP, itunes helper et What else can i do? Please help:(

    Now it sometimes keeps coming up and working but once i plug an ipod in it freezes my whole computer up and no i dont get any response from itunes at all

  • Why is it when i update my status on facebook from iohone it splits the updates into two differents boxes with some funky symbol. I have uninstall facebook and installed it over and it still does it. only the facebook IPHONE app.

    why is it when i update my status on facebook from iphone it splits the updates into two differents boxes with some funky symbols at the beginning.. I have uninstall facebook and installed it over and it still does it. only the facebook IPHONE app where it says to update staus text to FBOOK  i have already update my phone too.
    if i go on the facebook site and update there through a brower on iphone it will work fine just cant us IPHONE mobile upload,  . But its a pain of not using my IPHONE APP for facebook cause it does that.

    The warranty entitles you to complimentary phone support for the first 90 days of ownership.

  • I am going to buy a Mac desktop and want to know how to transfer all my purchases and uploaded music from my PC. How can I do this to ensure all videos and music transfer over and not just purchases?

    I am going to buy a Mac desktop and want to know how to transfer all my purchases and uploaded music from my PC. I originally started using iTunes on my PC so it has everything I have ever purchased or uploaded from my own collection of music.
    How can I do this to ensure all videos and music transfer over and not just purchases? Do they do it at the Apple Store or at Best Buy? help!

    Move iTunes Library from PC to MAC
    http://www.macworld.com/article/146958/2010/03/move_itunes_windows_mac.html
    Also, See here if you need to...  iTunes to an External Drive
    http://support.apple.com/kb/HT1751

  • I just recently purchased a mini iPad for my child. It was set up in the Apple store with my Apple id. I now want to create an Apple id for my child and switch it over, how can I go about that. I already downloaded some of the games from my account.

    I just recently purchased a mini iPad for my child. It was set up in the Apple store with my Apple id. I now want to create an Apple id for my child and switch it over, how can I go about that. I already downloaded some of the games from my account.

    With a new account, you'll need to rebuy the games.
    Each user should have their own icloud account, otherwise they end up getting the same emails, contacts, calendars, notes, reminders, etc. - usually not what you want.  But if all have been sharing the same itunes ID, keep it that way, you can have different IDs for itunes and icloud.
    If you already have another icloud account, and want to set it up on a device, then go to Settings>icloud, scroll to bottom of screen and tap Delete Account.  This only disconnects the device from that account, no data is lost on icloud.  Then enter the account ID that you want to use.
    To create a new icloud account, go to
    http://www.apple.com/icloud/setup/

  • My ipad has gone in lost mode by mistake and now i have it and i enter the password and i cant enter my ipad how can i enter or restart the ipad and start back over new

    my ipad has gone in lost mode by mistake and now i have it and i enter the password and i cant enter my ipad how can i enter or restart the ipad and start back over new

    FORCE IPAD INTO RECOVERY MODE
    1. Turn off iPad
    2. Turn on computer and launch iTunes (make sure you have the latest version of iTune)
    3. Plug USB cable into computer's USB port
    4. Hold Home button down and plug the other end of cable into docking port.
    DO NOT RELEASE BUTTON until you see picture of iTunes and plug
    5. Release Home button.
    ON COMPUTER
    6. iTunes has detected iPad in recovery mode. You must restore this iPad before it can be used with iTunes.
    7. Select "Restore iPad"...
    Note:
    1. Data will be lost if you do not have backup
    2. You must follow step 1 to step 4 VERY CLOSELY.
    3. Repeat the process if necessary.

  • My wife and i both have an itunes library installed on to our separate pc's, we are now de-commissioning the pc's and are moving over to a single mac mini, can we install both libraries on to the mac mini and run them as separate libraries?

    my wife and i both have an itunes library installed on to our separate pc's, we are now de-commissioning the pc's and are moving over to a single mac mini, can we install both libraries on to the mac mini and run them as separate libraries?

    graham218 wrote:
    are moving over to a single mac mini, can we install both libraries on to the mac mini and run them as separate libraries?
    Generally YES.
    It's possible to hold separate iTunes library's on a single machine.
    Generate two new folders inside the Music folder of the Mini and name them differently; e.g. man and woman.
    Copy your whole library into "man" and your wifes into "woman"
    Go to sharing in the system settings of the Mini and share the Music folder in your network.
    After copying the whole library's to the Mini you may point iTunes to the new location at startup by holding the option key (alt) at the keyboard.
    iTunes will then ask you where the library is located.
    Point it to the matching library on the Mini.
    That's all.
    Lupunus
    PS ... If you both use only the Mini you have to setup a user account for each and just copy the whole library to the Music folder in particular.
    Message was edited by: lupunus

  • How can i get my items to stay in place and not cross over each other when readjusting the browser s

    How can i get my items to stay in place and not cross over each other when readjusting the browser size.
    Basically on my site when i go from a small screen to a big screen everything doesn't adjust to the screen size. I don't know what im missing
    Here's the link to the page all the pages & they all do it
    http://theatricalworkslive.com/
    Thanks in advance

    Please read this whole message before doing anything.
    This procedure is a diagnostic test. It’s unlikely to solve your problem. Don’t be disappointed when you find that nothing has changed after you complete it.
    The purpose of the test is to determine whether the problem is caused by third-party software that loads automatically at startup or login, or by a peripheral device. 
    Disconnect all wired peripherals except those needed for the test, and remove all aftermarket expansion cards. Boot in safe mode and log in to the account with the problem.  Note: If FileVault is enabled, or if a firmware password is set, or if the boot volume is a software RAID, you can’t do this. Post for further instructions.
    Safe mode is much slower to boot and run than normal, and some things won’t work at all, including wireless networking on certain Macs. The next normal boot may also be somewhat slow.
    The login screen appears even if you usually log in automatically. You must know your login password in order to log in. If you’ve forgotten the password, you will need to reset it before you begin. Test while in safe mode. Same problem? After testing, reboot as usual (i.e., not in safe mode) and verify that you still have the problem. Post the results of the test.

  • HT201365 Just dowloaded the new ios 7, and i can't seem to get any apps to download in the app store. It doesn't have the meter bar anymore and all it does is have a Blue circle spin and spin for over 5 or 10 min. without nothing happening! Help !!!

    Just downloaded the new ios 7, and I can't seem to get any apps to download in the app store. It doesn't have the meter bar anymore and all it does is have a blue circle spin and spin for over 5 min. without nothing happening! Help!!!

    Hi,
    This is how App Store app shows now when an app is downloading. If you cannot download anything, go to Settings > iTunes & App Store, remove your Apple ID, and set it again. That should work.
    Hope that helps.

  • I have the iphone 4 and PC with Windows Vista Basic. When I bought my iPhone syncs with iTunes on my computer, but now when I try to sync it tells me that Mobile Device Service was closed and I can not synchronize the phone. Try restarting Mobile Device S

    I have the iphone 4 and PC with Windows Vista Basic. When I bought my iPhone syncs with iTunes on my computer, but now when I try to sync it tells me that Mobile Device Service was closed and I can not synchronize the phone. Try restarting Mobile Device Service and uninstall and reinstall Itunes but still giving me the same error.

    http://support.apple.com/kb/HT3965

  • I am trying to burn DVDs from iPhoto slideshows.  My pictures number in the hundreds and we know that iPhoto captures 99 and then "starts over".  This plays fine from the Mac Book but glitches arise on the DVD when playing it on a DVD player.  Any suggsts

    I am trying to burn DVDs from iPhoto slideshows.  My pictures number in the hundreds and we know that iPhoto captures 99 and then "starts over".  This plays fine from the Mac Book but glitches arise on the DVD when playing it on a DVD player.  Any suggestions on how to overcome the 99 "Chapter limitation" that IPhoto has?  Has anyone heard or tried an app called Vilisoft?  I read good things but I am not sure if it will do what I want.
    Jim

    Then use multipel 99 picture slideshows. 
    You can have up to 8 or 9 slideshows per menu.  If you add a sub menu reduce that by 1 and add 8 more to the sub menu.  The top menu can have links to up to 8 or so submenus (depends on the theme) and each submenu can have up to 8 or so slideshows. 
    However, converting a very large slideshow to a QT movie as Terence suggested does not envoke the 99 slide limit in iDVD.  It just becomes a single video/movie file.  So you should not have a problem with exporting at the large size, 720 x 540,
    and dragging the resulting file into the iDVD menu being sure to avoid any drop zones.
    OT

Maybe you are looking for