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


Groups > comp.lang.java.programmer > #22527

Concurrency and restarting tasks

Newsgroups comp.lang.java.programmer
Date 2013-02-26 06:32 -0800
Message-ID <7024d5a0-10c2-41db-bd72-85d03c3feae6@googlegroups.com> (permalink)
Subject Concurrency and restarting tasks
From me 2 <winona_whitener@yahoo.com>

Show all headers | View raw


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 at 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 task won't always take X seconds--sometimes it will generate exceptions or take longer or shorter or any number of other things.

Right now I have:

<code>
		final Runnable beeper = 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 = 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();
			}
</code>

There are plenty of examples that show the first steps--setting up the schedule and canceling.  Restarting apparently is not as common.

Any ideas would be fantastic.
Thank you,
Me

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Concurrency and restarting tasks me 2 <winona_whitener@yahoo.com> - 2013-02-26 06:32 -0800
  Re: Concurrency and restarting tasks Roedy Green <see_website@mindprod.com.invalid> - 2013-02-26 06:47 -0800
  Re: Concurrency and restarting tasks Joerg Meier <joergmmeier@arcor.de> - 2013-02-26 15:58 +0100
    Re: Concurrency and restarting tasks me 2 <winona_whitener@yahoo.com> - 2013-02-26 08:09 -0800
      Re: Concurrency and restarting tasks Joerg Meier <joergmmeier@arcor.de> - 2013-02-26 18:14 +0100

csiph-web