3

I have particular situation in our production environment, where a particular piece of code goes into an infinite loop. The cause is mostly data specific and not able to figure out the true cause. In the mean time, what I am hoping to do is spawn a separate child thread to execute that piece of code, and if it executes for say more than 30s, want to stop that child thread from executing

Thread t = new Thread() {
    public void run() {
                    // This is where i will the method that runs in a infinite loop
                    callMethodThatRunsInInfiniteLoop();
    };
};

t.start();
try {
    t.join(2000); // wait 2s
} catch (InterruptedException e) {
    e.printStackTrace();
} 

// if not completed how to break the child thread ???
3
  • 2
    You can try Thread.stop if you absolutely must, but you should really try to take advantage of interrupts. Commented Aug 8, 2012 at 22:59
  • isn't the Thread.stop() deprecated? Using it in Appserver environment like Jboss AS 7, are there any issues? Can't use interrupt because thread is never waiting on anything and the code that causes infinite loop is in a thirdparty library, which i can't modify. Commented Aug 8, 2012 at 23:04
  • Yes it is, which is why I stressed you should only use if you are forced to. Commented Aug 8, 2012 at 23:07

4 Answers 4

1

unfortunately no it is a thirdparty code, and will not be able to change it so easily.

It sounds like you are trying to work around a problem in the third-party library ... or in your code calling the third-party library with bad input or something.

My advice would be to fix THAT problem, rather than trying to kill the errant thread(s). And if you can't do that, then you have two choices:

  • modify the 3rd party library to be interrupt aware/responsive, and then use that to stop it, or

  • try to find a way to "reach into" the 3rd party library's data structures (e.g. using nasty reflection) and cause it to die.

If you are resorting to the latter, then maybe you should also look at running the 3rd party library in a separate JVM so that you can forcibly kill it using the Process API.


Having said that, there are limited circumstances where Thread.stop() is actually (probably) safe to use. Basically, if the errant thread doesn't create child threads, doesn't interact with other threads and doesn't share data structures, and can't be doing class initialization when you kill it, then you are probably going to be safe. The problem is there are so many theoretical scenarios where stopping the thread could cause damage that it is hard to know that you've considered all of them.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Agree that fixing the core problem would be best. Nice idea about using nasty reflection to force an ugly death. (yes, this is getting very very hacky) Another scenario where Thread.stop() can be bad is if that thread is obtaining locks, or opening Files, in the middle of writing or deleting a file, opening Streams, Sockets etc. In other words, it's rare that Thread.stop() is safe!
Thanks for the suggestions. The thirdparty api that goes into a loop is because of some bad input, and as funny as it might sound, it loops while trying to construct and error message with toString() on an Exception. Would love to fix the code or the input (just too many inputs), only if i could tell what the problem is. Its a hard to reproduce problem, and happened only twice so far. The above logic, is to identify the problem when it happens. I have gone with Thread.stop() as veer suggested and hopefully is a short term solution.
1

Is there a way to cause the infinite loop code to break by throwing an exception? e.g. set some variable to null? A possible advantage is that the affected thread (if well written) will clean up after itself and shut down more nicely than a stop().

1 Comment

unfortunately no it is a thirdparty code, and will not be able to change it so easily.
0

All methods for stopping a thread 'purely externally' are deprecated and considered unsafe. The only way a thread can be safely stopped requires

  1. modifying the code which is running to check whether it has been politely asked to stop via some stopMe variable being set; or
  2. co-opting a variable the other thread already uses to cause the thread to quit (which is generally very bad practice); for example, by forcing it to throw an exception, as suggested by user949300.

Without this, you have no choice but to use an unsafe method, which means Thread.stop(), which is also very bad practice. This is a very bad idea, and of course the only real solution is to either change your input so that this doesn't happen, or get the third-party code fixed.

Any objects the thread was using may be in an inconsistent (and possibly unusable) state, so try to avoid letting the Thread modify anything important, and don't look at any of its output variables if you can help it.

The Thread.stop() method still seems to exist in Java SE 7, though is of course deprecated, but I can't vouch for your particular environment.

See http://docs.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

Comments

0

Depending on the implementation of the method, you could start it in its own JVM, then stop it by calling destroy() on the process:

Process process = new ProcessBuilder("java", "-cp", "/some/class/path", "com.mycompany.MyLauncher").start();

// sometime later
process.destroy();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.