Results 1 to 10 of 10
  1. #1
    Bartman is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default SwingUtilities: invokeLater vs invokeAndWait vs SwingWorker

    I have code that receives an update every 10 ms which is to be graphed. Originally this code received an external event (subscription) when new data came in. Now I must 'pull' it - I created a Thread to receive the data (via Socket.receive). My problem lies in transferring that data to the EDT. As I understand it, I have these options:
    1. invokeLater. This method seemed to drop packets. I wasn't getting everything graphed. Unusable, but real-time was ok.
    2. invokeAndWait. This method graphed everything but quickly fell behind the real-time. I changed the graphics update manager (code I wrote when this thing ran on very slow laptops) to once per 150 ms and it worked fine. It's a bit jumpy but the data is accurate. I assume the overhead of creating the invokeAndWait every 10 ms is killing real-time.
    3. SwingWorker. I haven't tried this method yet; I have never used it. I'm not sure it is well-suited for this problem. Perhaps I am wrong. My thread will live forever, so there is no 'done'. I'm not sure how much that matters.


    So my question is, what method(s) have you used to transfer real-time data from an independent thread to the EDT?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: SwingUtilities: invokeLater vs invokeAndWait vs SwingWorker

    A SwingWorker is for going the other direction.
    That is, it's a way for launching a worker thread from the EDT, so it doesn't lock up the GUI.

    Not sure why invokeLater was dropping calls. It's the one I would have expected to work.
    I would suggest trying some debugging to see exactly what's going on.
    I suspect something is not quite right on the GUI side.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: SwingUtilities: invokeLater vs invokeAndWait vs SwingWorker

    I see a third option: write the incoming data to a suitable data structure and use that in the painting method; repaint at uniform periods using a Swing Timer.

    I don't understand what you mean by a "graphics update manager" -- Swing handles double buffering all on its own, so I don't see where you need to do anything but call repaint().

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Bartman is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default Re: SwingUtilities: invokeLater vs invokeAndWait vs SwingWorker

    Quote Originally Posted by Tolls View Post
    A SwingWorker is for going the other direction.
    That is, it's a way for launching a worker thread from the EDT, so it doesn't lock up the GUI.

    Not sure why invokeLater was dropping calls. It's the one I would have expected to work.
    I would suggest trying some debugging to see exactly what's going on.
    I suspect something is not quite right on the GUI side.
    I was planning on using SwingWorker's doInBackground to replace my thread.
    Last edited by Bartman; 08-24-2016 at 10:31 PM.

  5. #5
    Bartman is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default Re: SwingUtilities: invokeLater vs invokeAndWait vs SwingWorker

    Quote Originally Posted by DarrylBurke View Post
    I see a third option: write the incoming data to a suitable data structure and use that in the painting method; repaint at uniform periods using a Swing Timer.
    I'm running on windows and the timers are horrible I convinced my boss that Linux is a better option (he agreed after a demo) but is worried about changing... long story. I tried this method and the graphics updates are way too jumpy (on Windows - it was great on Linux). The customer complained.

    Quote Originally Posted by DarrylBurke View Post
    I don't understand what you mean by a "graphics update manager" -- Swing handles double buffering all on its own, so I don't see where you need to do anything but call repaint().

    db
    Because we were running real-time data on very slow laptops (at one time - years ago) I wrote some code that ensured graphics updates took place only once per X milliseconds. The data would get way behind real-time without that. It has nothing to do with buffering, but keeping up with time.
    Last edited by Bartman; 08-24-2016 at 10:32 PM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: SwingUtilities: invokeLater vs invokeAndWait vs SwingWorker

    Can you provide a compileable example that demonstrates the graphics problem on Windows. I have never had a problem
    like you describe. Just keep it under 100 lines please. An example of the timer problem would be useful too.

    Edit: Actually, I did have a problem with the timers (not the graphics). So I ended up writing my own timer using a
    tight loop (tweakable though) using the System. nanoTime() method. I checked this out and was able to run
    multiple timers with a delay of about 2 ms.

    Regards,
    Jim
    Last edited by jim829; 08-25-2016 at 03:14 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: SwingUtilities: invokeLater vs invokeAndWait vs SwingWorker

    Quote Originally Posted by Bartman View Post
    I was planning on using SwingWorker's doInBackground to replace my thread.
    Doesn't sound like your thread is the issue.
    Sounds like it's getting your thread to communicate at a sufficient rate with the EDT.

    No harm in trying the Worker of course, but I would expect anything done in the EDT is queued the same as invokeLater.
    That is, of course, an assumption...
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Bartman is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default Re: SwingUtilities: invokeLater vs invokeAndWait vs SwingWorker

    Quote Originally Posted by jim829 View Post
    Can you provide a compileable example that demonstrates the graphics problem on Windows. I have never had a problem
    like you describe. Just keep it under 100 lines please. An example of the timer problem would be useful too.
    I can't; the code is much larger than 100 lines, and my employer would flog me if they found out :(

    Quote Originally Posted by jim829 View Post
    Edit: Actually, I did have a problem with the timers (not the graphics). So I ended up writing my own timer using a
    tight loop (tweakable though) using the System. nanoTime() method. I checked this out and was able to run
    multiple timers with a delay of about 2 ms.
    Interesting. Are those 'Swing' timers?

    The solution to my problem should be fairly simple but I fear I am too wrapped up in the minutiae of the code to see it. The events are received from a device that uses an RTOS so their timing is once per 10 ms exactly. When my (previous) code received an event from them, it ran fine (the events were received on the EDT, no blocking). We had to change it to that device sending data as a broadcast to the network and my code cannot receive that data on the EDT else it will block the menus' operability. So I receive it on a different thread. Getting it from that thread to the EDT should be simple. I tried the observer/observable pattern, hoping that since the observer was created on EDT and the observable on my thread it would work but the 'update' ran on my thread not the EDT.

    Any ideas? I will consider Jim's thought while waiting for inspiration.


    P.S. The graphics updates are very CPU intensive: the data is graphed to 14 different strip charts, 100 times per second.
    Last edited by Bartman; 09-21-2016 at 05:56 PM. Reason: additional detail

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: SwingUtilities: invokeLater vs invokeAndWait vs SwingWorker

    Well, I wrote a little toy to generate some data in one thread, fire an event when the data was ready (every 2 ms), and then plot it.
    The data is simply a moving sine wave. There are two primary threads involved, the generating thread and the EDT.

    The response seems to be fine and there does not appear to be loss of data. And the EDT does not interfere with other EDT based
    operations. So I am not certain why you had menu problems. Of course any set of operations on the EDT should be done as
    quickly as possible to avoid contention with other EDT based operations. So most processing destined for EDT handling should
    be done outside the EDT.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: SwingUtilities: invokeLater vs invokeAndWait vs SwingWorker

    Quote Originally Posted by Bartman View Post
    [*] SwingWorker. I haven't tried this method yet; I have never used it. I'm not sure it is well-suited for this problem. Perhaps I am wrong. My thread will live forever, so there is no 'done'. I'm not sure how much that matters.
    That's a good idea. This is the method you need and it should make it pretty easy. Well documented in API:
    https://docs.oracle.com/javase/8/doc...#publish-V...-

Similar Threads

  1. EDT Invokeandwait
    By paulrussell70 in forum AWT / Swing
    Replies: 8
    Last Post: 11-25-2011, 10:34 PM
  2. SwingUtilities.invokeLater();
    By Lil_Aziz1 in forum New To Java
    Replies: 4
    Last Post: 02-08-2010, 10:11 PM
  3. what is SwingUtilities.invokeLater(new Runnable())
    By sivakumar_sakam in forum AWT / Swing
    Replies: 3
    Last Post: 05-06-2009, 04:47 AM
  4. Use of swingutilities.invokeLater
    By neha_negi in forum AWT / Swing
    Replies: 2
    Last Post: 09-19-2008, 02:48 AM
  5. Replies: 0
    Last Post: 07-03-2008, 08:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •