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


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

Concurrency and restarting tasks

Started byme 2 <winona_whitener@yahoo.com>
First post2013-02-26 06:32 -0800
Last post2013-02-26 18:14 +0100
Articles 5 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#22527 — Concurrency and restarting tasks

Fromme 2 <winona_whitener@yahoo.com>
Date2013-02-26 06:32 -0800
SubjectConcurrency and restarting tasks
Message-ID<7024d5a0-10c2-41db-bd72-85d03c3feae6@googlegroups.com>
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

[toc] | [next] | [standalone]


#22528

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-02-26 06:47 -0800
Message-ID<6iipi811f7jngh0hhvgftpa3vtj45qcmrl@4ax.com>
In reply to#22527
On Tue, 26 Feb 2013 06:32:28 -0800 (PST), me 2
<winona_whitener@yahoo.com> wrote, quoted or indirectly quoted someone
who said :

> I=
> want to be able to cancel the execution of that task.

To kill cleanly, the task must periodically check if it should quit
and commit suicide at a convenient point after cleaning up.

See StoppableThread, part of
http://mindprod.com/products1.html#COMMON11
-- 
Roedy Green Canadian Mind Products http://mindprod.com
One thing I love about having a website, is that when I complain about
something, I only have to do it once. It saves me endless hours of 
grumbling.

[toc] | [prev] | [next] | [standalone]


#22529

FromJoerg Meier <joergmmeier@arcor.de>
Date2013-02-26 15:58 +0100
Message-ID<5w697krlf571$.1hwa9p5it81zl.dlg@40tude.net>
In reply to#22527
On Tue, 26 Feb 2013 06:32:28 -0800 (PST), me 2 wrote:

> 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.

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

As I understand, ThreadS are simply not made to be resumed after being
cancelled. What you want might simply not be possible. That being said, it
shouldn't be too hard to simply make a new Thread with your Runnable and
start that instead of restarting the cancelled one.

Alternatively, you can make your long running task somehow check for a
volatile flag and pause if it's set to something.

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#22536

Fromme 2 <winona_whitener@yahoo.com>
Date2013-02-26 08:09 -0800
Message-ID<55b9dfa9-2e79-4e42-b5fb-de6a44db9188@googlegroups.com>
In reply to#22529
On Tuesday, February 26, 2013 9:58:28 AM UTC-5, Joerg Meier wrote:
> On Tue, 26 Feb 2013 06:32:28 -0800 (PST), me 2 wrote:
> 
> 
> 
> > 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.
> 
> 
> 
> > There are plenty of examples that show the first steps--setting up the schedule and canceling.  Restarting apparently is not as common.
> 
> 
> 
> As I understand, ThreadS are simply not made to be resumed after being
> 
> cancelled. What you want might simply not be possible. That being said, it
> 
> shouldn't be too hard to simply make a new Thread with your Runnable and
> 
> start that instead of restarting the cancelled one.
> 
> 
> 
> Alternatively, you can make your long running task somehow check for a
> 
> volatile flag and pause if it's set to something.
> 
> 
> 
> Liebe Gruesse,
> 
> 		Joerg
> 
> 
> 
> -- 
> 
> Ich lese meine Emails nicht, replies to Email bleiben also leider
> 
> ungelesen.

Hey,

I suppose you are right.  That doesn't make much sense when you put it like that.

The problem is that I need an event to fire if the task doesn't complete in X seconds.  I guess I don't really care about canceling the thread--just getting the event to fire and then getting set up to attempt the task again.

The double setup with the two scheduled tasks--one to start and one to cancel--gets muddled after like the 4th iteration.

Hmmm....

I'll keep looking.
Me

[toc] | [prev] | [next] | [standalone]


#22542

FromJoerg Meier <joergmmeier@arcor.de>
Date2013-02-26 18:14 +0100
Message-ID<698aitci6c9k$.cvd64r9xhn1i$.dlg@40tude.net>
In reply to#22536
On Tue, 26 Feb 2013 08:09:14 -0800 (PST), me 2 wrote:


> The problem is that I need an event to fire if the task doesn't complete in X seconds.  I guess I don't really care about canceling the thread--just getting the event to fire and then getting set up to attempt the task again.

> The double setup with the two scheduled tasks--one to start and one to cancel--gets muddled after like the 4th iteration.

If I understand your problem correctly, the following should be helpful -
but do read why Thread.stop was deprecated. It would be much, much better
if you could change your design to avoid needing it. With that disclaimer:

package com.usenet.watchdog;

import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class WatchDog {
	private final long				LIMIT	= 5000;																																// 10
seconds

	private final DateFormat	DF		= new SimpleDateFormat("mm 'minutes,' ss
'seconds,' S 'milliseconds'");
	private final long				START	= System.currentTimeMillis();

	private WorkerThread			worker;

	private String timestamp() {
		return DF.format(new Date(System.currentTimeMillis() - START));
	}

	private class WorkerThread extends Thread {
		private final Object	workload;
		private final long		started	= System.currentTimeMillis();

		public WorkerThread(final Object workload) {
			this.workload = workload;
		}

		@Override
		public void run() {
			System.out.println(timestamp() + " - (WorkerThread) started");
			System.out.println(timestamp() + " - (WorkerThread) doing stuff with " +
workload);
			while (true) {
				if (System.currentTimeMillis() % 1000 == 0) {
					System.out.println(timestamp() + " - (WorkerThread) ping");
				}
			}
		}
	}

	private class WatchdogThread extends TimerTask {
		@Override
		public void run() {
			System.out.println(timestamp() + " - (WatchdogThread) checking if worker
thread is over the time limit");
			if (System.currentTimeMillis() - worker.started > LIMIT) {
				System.out.println(timestamp() + " - (WatchdogThread) worker has been
working too long, resetting it");
				worker.stop();
				final Object workload = worker.workload;
				worker = new WorkerThread(workload);
				worker.start();
			}
		}
	}

	public static void main(final String[] args) {
		new WatchDog().start();
	}

	private void start() {
		System.out.println(timestamp() + " - (main) Starting run");
		worker = new WorkerThread(new Socket());
		worker.start();
		final Timer timer = new Timer();
		timer.scheduleAtFixedRate(new WatchdogThread(), 1000, 863);
	}
}

A somewhat more readable version, including some example output, can be
seen here:

<http://pastebin.com/5gcLPS8D>

And a good writeup of why not to use Thread.stop - basically, any objects
the stopped Thread touched (in this case workload) could be damaged beyond
repair. Read more at:

<http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html>

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web