1

I have a complex function (optimisation) that can potentially enter in a loop or just to take too much time, and the time allowed is set by the user.

Therefore I am trying to make to run the function in a separate thread, and to stop it if the maximum time is passed. I use a code similar to the one below, but it doesn't work, so

int timeMax = 2; //time in minutes  
Thread Thread_Object = new Thread_Class(... args...);
try {
  Thread_Object.start();
  Thread_Object.join(timeMax*60*1000);
}

I think that I'm not using the function "join" properly, or it doesn't do what I have understood. Any idea?

Thanks!


Thanks for the answers, currently I have found a better idea here*. It works but it still uses the function "stop" that is deprecated. The new code is:

Thread Thread_Object = new Thread_Class(... args...);

try {
  int timeMax = 1;
  Thread_Object.start();
  Thread.currentThread().sleep( timeMax * 1000 );

    if ( Thread_Object.isAlive() ) {
        Thread_Object.stop();
        Thread_Object.join();
    }

}
catch (InterruptedException e) {

}

not yet sure of the function of "join", I'll have to go to have a look at some book.

Community
  • 1
  • 1
Andrew Strathclyde
  • 327
  • 4
  • 10
  • 23
  • 1
    Dont't use `Thread.stop()`. It can leave shared data in an inconsistent state. If you're not going to use a `Timer`, I suggest you do what John V said. – Jeremy Nov 16 '10 at 14:05

5 Answers5

3

The join method will wait the current thread until the thread that is being joined on finishes. The join with milliseconds passed in as a parameter will wait for some amount of time, if the time elapses notify the waiting thread and return.

What you can do, is after the join completes interrupt the thread you joined on. Of course this requires your thread to be responsive to thread interruption.

John Vint
  • 39,695
  • 7
  • 78
  • 108
  • `interrupt()` only works if thread is waiting (is blocked by `wait(..)` or `join(..)`). – Peter Knego Nov 15 '10 at 16:21
  • That is under the assumption you don't write code that handles interruption. You can easily have a check if(Thread.currentThread().isInterrupted()){ thread.interrupt(); return; } – John Vint Nov 15 '10 at 16:22
  • Please read: http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#interrupt() – Peter Knego Nov 15 '10 at 16:24
  • If a thread calls `wait()` or `join()` (on another thread) it can be externally interrupted by `interrupt()`. Otoh, if it is doing some work, than `interrupt()` will do nothing. – Peter Knego Nov 15 '10 at 16:26
  • Peter, your resolution is to have a boolean flag to be set if the thread should stop. Would testing to see if a thread is interrupted be the same thing? – John Vint Nov 15 '10 at 16:27
  • 1
    Not entirely: you need to check the flag with `interrupted()` (warrning: this clears the flag), AND handle `InterruptedException`. – Peter Knego Nov 15 '10 at 16:40
  • 1
    Though that is true, you are not taking into account my previous comment. I specifically said if he checks Thread.currentThread().isInterrupted() then he can assume the thread should be stopped. Its also important to know isInterrupted() does NOT effect the interruption status – John Vint Nov 15 '10 at 16:45
  • Interruption status is not set if thread is inside `wait()` - the docu link I posted explains this. Note that `wait()` is used by most InputStream operations waiting for data. In this case thread will get `InterruptedException` but no interruption flag will be set. So, he must check both interruption flag AND catch `InterruptedException`. – Peter Knego Nov 15 '10 at 17:15
  • Of course he has to handle InterruptedException. That goes in hand to what I said when I mentioned he has to be responsive to interruption. My solution is still more effective to the single flag, because if his thread is waiting setting the flag to true will do nothing to stop the thread – John Vint Nov 15 '10 at 17:48
  • And at that point the exception is thrown he now knows the thread is interrupted and can leave the thread accordingly – John Vint Nov 15 '10 at 17:50
3

I suggest you use a Timer.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
1

Thread.join(milis) does not kill the thread. It just waits for the thread to end.

Java threading is cooperative: you can not stop or gracefully kill a thread without it's cooperation. One way to do it is to have an atomic flag (boolean field) that thread is checking and exiting if set.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
1

Watchdog-Timers in Java are not a simple thing, since threading is cooperative. I remember that in one project we just used Thread.stop() although it is deprecated, but there was no elegant solution. We didn't face any issues using it, though.

A good example for a Java Watchdog implementation:

http://everything2.com/user/Pyrogenic/writeups/Watchdog+timer

Jules
  • 1,352
  • 7
  • 19
0

This might be useful

http://tempus-fugit.googlecode.com/svn/site/documentation/concurrency.html#Scheduled_Interruption

Toby
  • 9,523
  • 8
  • 36
  • 59