1

I'm trying to setup a "check-in" thread in my program that will throw an exception if another thread doesn't perform a "check-in" command before time is up. If another thread does check-in before time is up, the timer resets and listens for another check-in.

I'm not overly familiar with Java's Timer object, but I'm assuming the best way to do this would be using the Timer, as the only other method I can think of is starting a new thread with a Thread.sleep(time) and interrupting it before it wakes, then re-instantiating/starting it; if it gets through the Thread.sleep(time) it throws the exception. This seems extremely crude and inefficient, though.

So, the basic idea is this:

  • Start some kind of timer with a "timeout" of 60 seconds.
  • If the timeout is reached, an exception is thrown.
  • If another thread "checks-in" before timer times out, the timer resets.

How can I properly do this?

DGolberg
  • 2,109
  • 4
  • 23
  • 31
  • 1
    Where would the exception be thrown? – Chris K Sep 23 '14 at 21:57
  • You may want to also look into a Thread Executor in Java. Very similar to the Timer but with a bit more functionality. – gnomed Sep 23 '14 at 21:58
  • 1
    You need to use a `ScheduledExecutorService`. Should be very simple. Why do you need this anyway? A `Thread` doesn't just die... – Boris the Spider Sep 23 '14 at 21:59
  • I'm using it to monitor an external program that may randomly get "stuck" due to an alert prompt. The program is on a remote system, so it's hard to keep an eye on it at all times. Usually we find the issue when a project it's supposed to process doesn't come out the other end for a couple hours. The program can communicate with Java via JavaScript by opening a socket, but since it's only single-threaded, it made more sense to have it "report in" every so often (the sub-tasks only take a few seconds, but the whole project could take an hour or more to process). – DGolberg Sep 23 '14 at 22:41

2 Answers2

2

Use a ScheduledExecutorService that returns a ScheduledFuture that can be cancelled:

private ScheduledExecutorService ses = 
    Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture<?> bomb = null;

public void reset() {
    if(bomb != null) {
        bomb.cancel();
    }
    boom = ses.schedule(new Runnable() {
        @Override
        public void run() {
            throw new IllegalStateException("Boom");
        }
    }, 60, TimeUnit.SECONDS);
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • Very nice! I like how this method resets immediately on a check-in rather than having to wait for a timer to finish. This will provide faster responses if/when a check-in is missed. – DGolberg Sep 24 '14 at 18:05
1

Create a class that extends java.util.TimerTask. Within the class, have a volatile boolean variable for whether a checkin has been performed. When the other thread checks in, it should set the boolean to true; in the class's run() method should check the boolean, throw an exception if it is false, and reset it to false if it is true.

Then, on program startup, create one object of your class and retain a reference to it for access by other threads. Create one java.util.Timer object, and use it to schedule the one object of your TimerTask subclass using the scheduleAtFixedRate() method.

Note that just throwing an exception will only terminate the thread the exception is thrown from; other threads will keep running. If you want more than just a stack trace in the log, you may need to initiate an application shutdown instead of throwing the exception.

Warren Dew
  • 8,790
  • 3
  • 30
  • 44
  • 1
    Ahh, the `TimerTask` was what I was missing when I looked at the other `Timer` examples. This should work for what I need to do. I'll see if I can come up with an implementation of it tomorrow. Thanks! – DGolberg Sep 23 '14 at 22:52
  • Very nice way to use Timers, though I had to give correct answer to Jean for a slightly faster alert due to an instant timer reset upon check-in. Still, I learned some things about timers, so +1 for the advice! – DGolberg Sep 24 '14 at 18:07
  • Thanks. You can do the same thing with a timer, but Jean took the trouble to provide actual code. – Warren Dew Sep 25 '14 at 15:04
  • Yeah, I realized that after I was playing around with my new found knowledge of timers for something else (thanks again, btw). It did make me curious as to the differences between the 2, though it appears the only difference is `Timer` uses a single thread while the other can start multiple. Timer looks slightly easier to setup, though. If I could accept more than one correct answer, I would, lol. – DGolberg Sep 25 '14 at 16:10