Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #23990 > unrolled thread

The invokeLater loop

Started byram@zedat.fu-berlin.de (Stefan Ram)
First post2013-05-10 18:54 +0000
Last post2014-01-30 20:48 -0800
Articles 12 — 10 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  The invokeLater loop ram@zedat.fu-berlin.de (Stefan Ram) - 2013-05-10 18:54 +0000
    Re: The invokeLater loop Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-05-10 12:18 -0700
      Re: The invokeLater loop Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-05-10 14:26 -0700
      Re: The invokeLater loop Joerg Meier <joergmmeier@arcor.de> - 2013-05-10 23:28 +0200
      Re: The invokeLater loop Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-05-10 17:39 -0400
      Re: The invokeLater loop Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-05-10 23:50 +0200
    Re: The invokeLater loop markspace <markspace@nospam.nospam> - 2013-05-10 14:51 -0700
    Re: The invokeLater loop Silvio <silvio@internet.com> - 2013-05-11 00:26 +0200
      Re: The invokeLater loop Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-05-10 15:52 -0700
    Re: The invokeLater loop Roedy Green <see_website@mindprod.com.invalid> - 2013-05-10 23:27 -0700
      Re: The invokeLater loop "Qu0ll" <Qu0llSixFour@gmail.com> - 2013-05-12 19:44 +1000
    Re: The invokeLater loop Knute Johnson <eternal@knutejohnson.com> - 2014-01-30 20:48 -0800

#23990 — The invokeLater loop

Fromram@zedat.fu-berlin.de (Stefan Ram)
Date2013-05-10 18:54 +0000
SubjectThe invokeLater loop
Message-ID<invokeLater-loop-20130510204735@ram.dialup.fu-berlin.de>
  I invented what I call the »invokeLater loop«:

class Object implements java.lang.Runnable
{ int i = 0;
  public void run()
  { java.lang.System.out.println( i++ );
    javax.swing.SwingUtilities.invokeLater( this ); }}

public class Main
{ public static void main( final java.lang.String[] args )
  { javax.swing.SwingUtilities.invokeLater( new Object() ); }}

  What is it good for? 

  Maybe it provides a way to loop while still allowing
  Swing events to be processed in parallel? (So you do not
  always have to spawn another thread besides the EDT.)

  One also might see it as a kind of recursion that will not
  overflow that stack. But since it does not atually nest
  incarnations, it is not really adopted for recursive programming.

[toc] | [next] | [standalone]


#23991

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2013-05-10 12:18 -0700
Message-ID<YJbjt.18$fS4.15@newsfe08.iad>
In reply to#23990
On 5/10/13 11:54 AM, Stefan Ram wrote:
>    I invented what I call the »invokeLater loop«:
>
> class Object implements java.lang.Runnable
> { int i = 0;
>    public void run()
>    { java.lang.System.out.println( i++ );
>      javax.swing.SwingUtilities.invokeLater( this ); }}
>
> public class Main
> { public static void main( final java.lang.String[] args )
>    { javax.swing.SwingUtilities.invokeLater( new Object() ); }}
>
>    What is it good for?
>
>    Maybe it provides a way to loop while still allowing
>    Swing events to be processed in parallel? (So you do not
>    always have to spawn another thread besides the EDT.)
>
>    One also might see it as a kind of recursion that will not
>    overflow that stack. But since it does not atually nest
>    incarnations, it is not really adopted for recursive programming.
>

A few comments:

That's isn't really a new invention.

It isn't recursive.

It encourages coupling the application logic to the UI environment.

It is a good way to eat up CPU cycles if you don't want to have your CPU 
go idle.  Also known as a laptop warmer.

It doesn't take advantage of concurrent processing capabilities of most 
modern systems, ultimately slowing down both your EDT and your 
application processing.

It is more error prone and harder to tune than using a SwingWorker.

Unrelated to the point you made Stefan, I feel I should make a note on 
Stefan Ram(TM) convention.  When I see "new Object()", and it isn't 
referring to java.lang.Object, it takes me 10 times as long to 
understand what the code is doing. I literally stared at that for 3 or 4 
seconds before it clicked that you were using confusing class names and 
hoping your abstruse convention would "clarify" the confusion.

[toc] | [prev] | [next] | [standalone]


#23994

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2013-05-10 14:26 -0700
Message-ID<uCdjt.437$Dd2.356@newsfe29.iad>
In reply to#23991
On 5/10/13 12:40 PM, Stefan Ram wrote:
> Daniel Pitts <newsgroup.nospam@virtualinfinity.net> writes:
>> It is a good way to eat up CPU cycles if you don't want to have your CPU
>> go idle.  Also known as a laptop warmer.
>
>    Say, I am writing an implementation of the »Game Of Life«. I have an
>    endless main loop whose contents updates to the next generation.
>
>    I want to have a sequence like:
>
>        - calculate next generation
>        - update screen
>        - calculate next generation
>        - update screeen
>        - calculate next generation
>        - ...
>
>    I think that I can do this by calculating the next generation and
>    updating the screen in »paintComponent« and then »invokingLater«
>    paintComponent or requesting a screen update which indirectly will
>    call »paintComponent« again. This will give the sequence above.
Depending on the type of simulation, you might not actually want to wait 
for the screen to update before continuing. This is actually 
particularly true in multi-player games.  GoL may be an exception of 
course, since typically users do want to see every frame.

By the way, calling "repaint()" doesn't mean the screen is updated. 
You'd have to be actively updating the screen each loop.

>    Another possibility might be to calculate the next generation in
>    a separate thread (SwingWorker). But I am not sure how to get
>    the above sequence (without waiting an arbitrary period of
>    times or complicated handshaking across thread boundaries).
>
Calling SwingWorker.invokeAndWait() isn't that complicated of a boundary 
IMO.

public class GoLMain {
    public static void main(String...args) {
       GameOfLifeSimulation gols = new GameOfLifeSimulation();
       GolUI ui = initUi();
       while (true) {
          Generation g = gols.nextGeneration();
          SwingUtilities.invokeAndWait(ui.generationPainterFor(g));
       }
    }
}


[toc] | [prev] | [next] | [standalone]


#23995

FromJoerg Meier <joergmmeier@arcor.de>
Date2013-05-10 23:28 +0200
Message-ID<1pnjvrbdetl0u.10sukmpahts14.dlg@40tude.net>
In reply to#23991
On Fri, 10 May 2013 12:18:15 -0700, Daniel Pitts wrote:

> Unrelated to the point you made Stefan, I feel I should make a note on 
> Stefan Ram(TM) convention.  When I see "new Object()", and it isn't 
> referring to java.lang.Object, it takes me 10 times as long to 
> understand what the code is doing. I literally stared at that for 3 or 4 
> seconds before it clicked that you were using confusing class names and 
> hoping your abstruse convention would "clarify" the confusion.

By this point, that must be intentional obfuscation, outright designed and
intended to cause bugs and present a maintenance nightmare, and you won't
convince me otherwise. Overwriting "Object" is so purely malicious that
there is no way that it's just random ineptitude, especially not from
Stefan, who certainly isn't clueless enough to not be aware of that.

My best guess would be that it's a straw man in the hopes of someone
complaining about a bug it caused, which could then be responded to with
something along the lines of "If only you would fully qualify every class
reference like me, this wouldn't have happened to you, this proves that
imports are evil".

But since Stefan generally does not address criticism of his coding style,
we will most likely forever be left to wonder what particular insanity
sparks the Stefan Ram(TM) convention ;)

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#23997

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-05-10 17:39 -0400
Message-ID<kmjp6q$ue0$1@dont-email.me>
In reply to#23991
On 5/10/2013 3:40 PM, Stefan Ram wrote:
> Daniel Pitts <newsgroup.nospam@virtualinfinity.net> writes:
>> It is a good way to eat up CPU cycles if you don't want to have your CPU
>> go idle.  Also known as a laptop warmer.
>
>    Say, I am writing an implementation of the »Game Of Life«. I have an
>    endless main loop whose contents updates to the next generation.
>
>    I want to have a sequence like:
>
>        - calculate next generation
>        - update screen
>        - calculate next generation
>        - update screeen
>        - calculate next generation
>        - ...
>
>    I think that I can do this by calculating the next generation and
>    updating the screen in »paintComponent« and then »invokingLater«
>    paintComponent or requesting a screen update which indirectly will
>    call »paintComponent« again. This will give the sequence above.

     How long does "calculate the next generation" take?

     - If it takes "significant" time, you're tying up the EDT
       and making the GUI unresponsive during the calculation.

     - If it takes "insignificant" time, you might as well just
       go ahead and do the deed without all the fuss and bother.

Either way, I see no use case for the scheme.  (Also, I don't
think you can rely on having one paintComponent() call per update:
If your GUI gets minimized everything might just freeze, or if
another window obscures part of your GUI you might get multiple
paintComponent() calls to refresh the display, or multiple calls
with different clip regions to paint around the intruder.  You
need some extra work to make sure paintComponent() only calls
invokeLater() "when appropriate.")

>    Another possibility might be to calculate the next generation in
>    a separate thread (SwingWorker). But I am not sure how to get
>    the above sequence (without waiting an arbitrary period of
>    times or complicated handshaking across thread boundaries).

     Leapfrogging a Runnable from EDT moment to EDT moment with
multiple invokeLater() self-invocations ...  What a splendid way
to avoid complication!

     It'd be simple enough to do it with a SwingWorker, if you
wanted to.  The GUI would create a SwingWorker instance to compute
one generation and call execute() to start it, running its
doInBackground() method on a worker thread.  When doInBackground()
finishes, the SwingWorker's done() method runs on the EDT, where
it can paint the display, and create and launch another SwingWorker
for the next generation.  Easy-peasy.

     Personally, I'd do it by driving things from the calculation
thread: Compute a new generation, compute the changes, send the
changes to the EDT via invokeLater() while the calculation thread
starts work on the next generation.  Easy to control the frames-
per-second rate, too, which I think would be horribly difficult
using EDT-multiplexing.

     Perhaps there's an application for the leapfrog technique, but
I've not yet heard or thought of one.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#23999

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2013-05-10 23:50 +0200
Message-ID<kmjps6$2li$1@dont-email.me>
In reply to#23991
On 10/05/2013 21:40, Stefan Ram allegedly wrote:
> Daniel Pitts <newsgroup.nospam@virtualinfinity.net> writes:
>> It is a good way to eat up CPU cycles if you don't want to have your CPU 
>> go idle.  Also known as a laptop warmer.
> 
>   Say, I am writing an implementation of the »Game Of Life«. I have an
>   endless main loop whose contents updates to the next generation.
> 
>   I want to have a sequence like:
> 
>       - calculate next generation
>       - update screen
>       - calculate next generation
>       - update screeen
>       - calculate next generation
>       - ...
> 
>   I think that I can do this by calculating the next generation and
>   updating the screen in »paintComponent« and then »invokingLater«
>   paintComponent or requesting a screen update which indirectly will
>   call »paintComponent« again. This will give the sequence above.

Not sure I get this. If you are performing non-UI-related computations
in your "Object" (I second Daniel's remarks on your choice of
nomenclature, btw), then these will happen on the EDT. But they should
actually be happening /off/ the EDT.

So it seems to me either you are busying the EDT and lagging the app, or
it's really just a laptop warmer. Perhaps another example would be useful.

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#24000

Frommarkspace <markspace@nospam.nospam>
Date2013-05-10 14:51 -0700
Message-ID<kmjpuu$33s$1@dont-email.me>
In reply to#23990
On 5/10/2013 11:54 AM, Stefan Ram wrote:
>
>    Maybe it provides a way to loop while still allowing
>    Swing events to be processed in parallel? (So you do not
>    always have to spawn another thread besides the EDT.)

Swing events can't be processed in parallel, they're run on a single thread.

[toc] | [prev] | [next] | [standalone]


#24002

FromSilvio <silvio@internet.com>
Date2013-05-11 00:26 +0200
Message-ID<518d742a$0$15873$e4fe514c@news2.news.xs4all.nl>
In reply to#23990
On 05/10/2013 08:54 PM, Stefan Ram wrote:
>    I invented what I call the »invokeLater loop«:
>
> class Object implements java.lang.Runnable
> { int i = 0;
>    public void run()
>    { java.lang.System.out.println( i++ );
>      javax.swing.SwingUtilities.invokeLater( this ); }}
>
> public class Main
> { public static void main( final java.lang.String[] args )
>    { javax.swing.SwingUtilities.invokeLater( new Object() ); }}
>
>    What is it good for?
>
>    Maybe it provides a way to loop while still allowing
>    Swing events to be processed in parallel? (So you do not
>    always have to spawn another thread besides the EDT.)
>
>    One also might see it as a kind of recursion that will not
>    overflow that stack. But since it does not atually nest
>    incarnations, it is not really adopted for recursive programming.
>

What is with this horrible code layout. Really makes makes me not want 
to read the code.

[toc] | [prev] | [next] | [standalone]


#24003

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2013-05-10 15:52 -0700
Message-ID<XSejt.462$li3.56@newsfe15.iad>
In reply to#24002
On 5/10/13 3:26 PM, Silvio wrote:
> [snip]
> What is with this horrible code layout. Really makes makes me not want
> to read the code.

Don't bother, Stefan is a long time poster refusing to cater to this 
audience.

It is better than some, however. Not quite "troll level". Try reading 
some of qwertymonkey's code for instance.

[toc] | [prev] | [next] | [standalone]


#24004

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-05-10 23:27 -0700
Message-ID<94pro8lgqi3bb0nnj923r453941589arst@4ax.com>
In reply to#23990
On 10 May 2013 18:54:38 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
wrote, quoted or indirectly quoted someone who said :

>  Maybe it provides a way to loop while still allowing
>  Swing events to be processed in parallel? (So you do not
>  always have to spawn another thread besides the EDT.)

You want Swing manipulation to run on the EDT and non Swing stuff to
run on some other threads.  invokeLater lets you put stuff on the
Swing thread from some non-Swing thread.  If you are already on the
Swing thread, you don't need it.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Nothing is so good as it seems beforehand. 
 ~ George Eliot (born: 1819-11-22 died: 1880-12-22 at age: 61) (Mary Ann Evans)

[toc] | [prev] | [next] | [standalone]


#24013

From"Qu0ll" <Qu0llSixFour@gmail.com>
Date2013-05-12 19:44 +1000
Message-ID<utidnXBzHvY5-RLMnZ2dnUVZ_q2dnZ2d@westnet.com.au>
In reply to#24004
"Roedy Green"  wrote in message 
news:94pro8lgqi3bb0nnj923r453941589arst@4ax.com...

In relation to SwingUtilities.invokeLater() you stated that:

>  If you are already on the Swing thread, you don't need it.

This is not strictly true, at least not in my experience.  Sometimes you are 
on the EDT and you want some code to be executed *after* all the events that 
are already queued on the EDT have completed.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
Qu0llSixFour@gmail.com
[Replace the "SixFour" with numbers to email me] 

[toc] | [prev] | [next] | [standalone]


#27430

FromKnute Johnson <eternal@knutejohnson.com>
Date2014-01-30 20:48 -0800
Message-ID<lcf9vh$up6$1@dont-email.me>
In reply to#23990
On 1/30/2014 18:44, Stefan Ram wrote:
> I wrote on 2013-05-10:
>> I invented what I call the »invokeLater loop«:
>> class Object implements java.lang.Runnable
>> { int i = 0;
>> public void run()
>> { java.lang.System.out.println( i++ );
>> javax.swing.SwingUtilities.invokeLater( this ); }}
>> public class Main
>> { public static void main( final java.lang.String[] args )
>> { javax.swing.SwingUtilities.invokeLater( new Object() ); }}
>> What is it good for?
>
>    I the meantime, I have actually used the invokeLater loop in
>    an animation. From the functional requirements point of view,
>    it did work. But it was by orders of magnitude slower than
>    the same animation with a for loop. OTOH, the for loop blocks
>    the EDT (if you do not want to start another thread, that is).
>    So, I ended up using the for loop as an inner loop to do
>    some iterations fast and then using invokeLater to call the
>    next iterations of the for loop, so that the EDT gets some
>    leeway to do other things. So I got my animation with a
>    single thread of execution, running fast and the EDT still
>    being responsive.
>

That doesn't make a lot of sense to me.  Calling invokeLater schedules a 
task on the EDT.  If you want something to run consistently, calling 
invokeAndWait would make more sense.  But given modern computers have 
multiple processors with multiple cores, it really makes more sense to 
use a separate thread and do active rendering off the EDT or to use 
invokeAndWait and keep your rendering times short.  Both will allow time 
for the EDT to process other things and give you fast and smooth animation.

-- 

Knute Johnson

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web