X-Received: by 10.224.208.9 with SMTP id ga9mr1052371qab.8.1361889148255; Tue, 26 Feb 2013 06:32:28 -0800 (PST) X-Received: by 10.49.75.195 with SMTP id e3mr1349380qew.24.1361889148220; Tue, 26 Feb 2013 06:32:28 -0800 (PST) Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!news.glorb.com!t2no5173872qal.0!news-out.google.com!t2ni1780qaj.0!nntp.google.com!dd2no3260746qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Tue, 26 Feb 2013 06:32:28 -0800 (PST) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=137.100.97.30; posting-account=LYoTOgoAAABkuGLLOGXg_LiQpwWAp52F NNTP-Posting-Host: 137.100.97.30 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <7024d5a0-10c2-41db-bd72-85d03c3feae6@googlegroups.com> Subject: Concurrency and restarting tasks From: me 2 Injection-Date: Tue, 26 Feb 2013 14:32:28 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.java.programmer:22527 Hey, I have an interesting (to me at least!) puzzle. I have a task that can take a long time that I need to run periodically. I= want to be able to cancel the execution of that task. I've been looking a= t the Scheduler and Future objects, but I haven't seen how to cleanly stop,= wait for a couple of seconds and restart the task on schedule. And my tas= k won't always take X seconds--sometimes it will generate exceptions or tak= e longer or shorter or any number of other things. Right now I have: final Runnable beeper =3D new Runnable() { public void run() { System.out.println("beep -- doing long task"); //doing long task System.out.println("finished doing long task"); } }; ScheduledFuture beeperHandle; try { beeperHandle =3D scheduler.scheduleAtFixedRate( beeper, 1, 2, SECONDS); beeperHandle.get(3, SECONDS); } catch (TimeoutException te) { System.out.println("Canceled due to timeout"); } catch (InterruptedException | ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } There are plenty of examples that show the first steps--setting up the sche= dule and canceling. Restarting apparently is not as common. Any ideas would be fantastic. Thank you, Me