How to stop uninterruptible threads in Java

I have a Java application that I CAN'T EDIT that starts a java.lang.Thread that has this run() method:

public void run(){
   while(true){
     System.out.println("Something");
   }
}

At a certain point in time I want to stop it. If I use Thread.interrupt() it doesn't work. If I use Thread.stop() it works, but this method is deprecated (so its use is discouraged as it may be removed from JVM in new releases).

How to stop such uninterruptible threads in Java?

Answers


You can implement the interrupt method using a check variable.

First of all use a volatile check variable as:

volatile boolean tostop = false; // Keep the initial value to be false

Next define your thread to be dependent to this variable.

Thread thread1 = new Thread(){
    public void run() {
        while(!tostop) {
         -- Write your code here --
        }
     }
 }

Next define the function in which to use the thread:

public void ....{
    //Interrupt code
    tostop = true;
    thread1.sleep(300);  // Give the thread sometime for cleanup
    //Use System.exit(0), if the thread is in main function.
}

Hope this helps.


The java debugger will allow you to kill a thread by injecting an exception into it.

Start your Java process and have it listen on some port:

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=4444 <Your Program>

and connect the debugger with something like:

jdb -attach 127.0.0.1:4444

and issue the following command:

threads

to get a list of the running threads, and use the kill command to kill a running thread.

kill 0xe2e new java.lang.IllegalArgumentException("er");

Under this link: http://download.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html:

What if a thread doesn't respond to Thread.interrupt?

In some cases, you can use application specific tricks. For example, if a thread is waiting on a known socket, you can close the socket to cause the thread to return immediately. Unfortunately, there really isn't any technique that works in general. It should be noted that in all situations where a waiting thread doesn't respond to Thread.interrupt, it wouldn't respond to Thread.stop either. Such cases include deliberate denial-of-service attacks, and I/O operations for which thread.stop and thread.interrupt do not work properly.


You can't reliably interrupt a thread without cooperation from that thread.

As an ugly hack (not for practical use!), you can substitute System.out and make println throw an exception when interruption condition is met and the thread in question is a current thread (i.e. use it as a hook to provide some cooperation in context of the target thread).

EDIT: Even more elegant option - you can make println interruptable by throwing an exception when Thread.interrupted() is true, so that the thread can be interrupted by calling thread.interrupt().

Semantic of this method would be very close to the ordinary interruptable methods (i.e. methods that throw InterruptedException), although you can't use InterruptedException since it's a checked exception and need to use unchecked RuntimeException instead:

System.setOut(new PrintStream(System.out) {
    public void println(String s) {
        if (Thread.interrupted()) throw new RuntimeException();
        super.println(s);
    }
});

Ok, I've found the solution and it's really really simple, just sleep the thread and then interrupt it!

final Thread t = getTheThreadToClose();

//Final is important because only in this way you can pass it to the following new Thread

if (t != null && t.isAlive() && !t.isInterrupted()){
    try{
       new Thread(){
           public void run(){
               try{ 
                  t.sleep(3000);//SLEEP INSIDE THE NEW THREAD
               }
               catch(InterruptedException ex){}
           }
       }.start();

       t.interrupt();//INTERRUPT OUTSIDE THE NEW STARTED THREAD
    }
    catch(Exception e){}
}

It works in all cases!

I wanna thank you all for your really useful help, especially venomrld and Toby that gave me inspiration for the solution with their words ;-), and this wonderfoul site that let me ALWAYS solve all my programming problems.

Thank you all again and Happy New Year!


I am not aware of any deprecated method which has been removed from the JRE ever. stop() has been deprecated a very long time, possibly more than 10 years. Its a bad idea to use it, but it may be your best option.


Need Your Help

Javascript getting progress of PHP-script

php javascript ajax

I need to provide interaction between js on html-page and php-script.

How to fields to the User model

python django user registration django-registration

I want to be able to save a picture to a user and maybe even a description, these can be optional, but how to you add to a model. I don't want to go into the souce code of django to change it, can I

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.