Path: csiph.com!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Nigel Wade Newsgroups: comp.lang.java.programmer Subject: Re: Interrupt code while running (from Form) Date: Thu, 20 Oct 2016 17:49:48 +0100 Lines: 15 Message-ID: References: <6628a02c-9c5a-4cdc-8113-fcf13c972ea4@googlegroups.com> <17cd471e-65d9-47ed-b616-be748efd8fec@googlegroups.com> <191335d2-ae49-4ab0-b502-1d71d62b6bc0@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net E6edGBIX7moN+wuaX1zceAO51izn/uQs40poLWdPvIp2ta5NhL Cancel-Lock: sha1:0TiFr0Y3kZzLJA4RpLzEzgcQzho= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 In-Reply-To: <191335d2-ae49-4ab0-b502-1d71d62b6bc0@googlegroups.com> Xref: csiph.com comp.lang.java.programmer:35853 On 20/10/16 16:47, Eric Douglas wrote: > On Thursday, October 20, 2016 at 11:02:02 AM UTC-4, Nigel Wade wrote: >> To perform background tasks in a Swing application it's preferable to use a SwingWorker. It takes care of worker thread management for you so you shouldn't get problems like this (the worker threads are daemons). It also has other advantages if any UI feedback is required. > > I'm not sure what you mean by the thread management. Thread management is the bit you've got wrong if you need to resort to jvisualvm to see why your program didn't finish. You've failed to manage the lifecycle of your threads, and some are still running, preventing the JVM from exiting cleanly. > As far as I know the SwingWorker should only be necessary if you are running something in a daemon thread that is going to need access to GUI. > It's [almost] necessary when doing that, but there's no reason you can't use it for any other background tasks in a Swing GUI application. The lifecycle methods in SwingWorker integrate very well with an event driven GUI. Otherwise you have to do everything manually, and if you get it wrong you end up messing with jvisualvm, or TaskManger or Ctrl-C to end your errant process/threads.