Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.roellig-ltd.de!open-news-network.org!fbe002.ams.xsnews.nl!feed.xsnews.nl!border04.ams.xsnews.nl!feeder02.ams.xsnews.nl!frontend-F09-05.ams.news.kpn.nl From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: Could this be done better Organization: Decebal Computing References: <87zjbg97c5.fsf@Equus.decebal.nl> <87ppcc8s3i.fsf@Equus.decebal.nl> X-Face: "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR,v+Pti8=Vi/Z"g^?b"E X-Homepage: http://www.decebal.nl/ Date: Tue, 25 Nov 2014 00:12:50 +0100 Message-ID: <877fyk6vm5.fsf@Equus.decebal.nl> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) Cancel-Lock: sha1:ZxyaTQiW60m2GzrBerO1aDV/lPY= MIME-Version: 1.0 Content-Type: text/plain Lines: 42 NNTP-Posting-Host: 77.171.42.144 X-Trace: 1416870835 news.kpn.nl 21160 77.171.42.144@kpn/77.171.42.144:54109 Xref: csiph.com comp.lang.java.programmer:30402 Op Monday 24 Nov 2014 19:42 CET schreef Eric Sosman: >>> - Use javax.swing.Timer instead of java.util.Timer. The >>> mechanics are a bit different -- you write an ActionListener >>> instead of subclassing TimerTask -- but the action listener >>> automatically runs on the EDT, where it's safe to manipulate >>> your GUI. >> >> Or is there a reason why did would be better? > > (Shrug.) For your purpose I don't see much difference. > But suppose you wanted to add a few more buttons to your GUI: > > "Next colour" > "Previous colour" > "Start cycling" > "Stop cycling" > > You'll need an ActionListener for the "Next colour" button, > and with javax.swing.Timer you could re-use that exact same > ActionListener for cycling. Essentially, the Timer would fire > the same kind of events the button does, and they'd be handled > by the same chunk of code. > > The JavaDoc for javax.swing.Timer mentions another possible > advantage: If you have a *lot* of timed activity in your GUI, > you might wind up creating a *lot* of java.util.Timer threads if > you weren't careful. With javax.swing.Timer that's not an issue; > javax.swing.Timer "multiplexes" all the different events on a > single internal thread. FWIW, I myself have never created so > many Timers (of either kind) that thread multiplicity ever began > to become a problem, but, hey ... > > An even more flexible and feature-ridden alternative would be > to use java.util.concurrent.ScheduledThreadPoolExecutor. Looks like I have some experimenting to do. :-D -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof