Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: The invokeLater loop Date: Thu, 30 Jan 2014 20:48:49 -0800 Organization: A noiseless patient Spider Lines: 37 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 31 Jan 2014 04:48:49 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="aba33539224e5c782fe0c4053f7756fd"; logging-data="31526"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/MUuqd7GISkUQryVxlxPd9" User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: Cancel-Lock: sha1:u7SGER7aszo2uvD/Yp9x9GiPsP4= Xref: csiph.com comp.lang.java.programmer:27430 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