How to stop uninterruptible threads in Java

后端 未结 6 1948
有刺的猬
有刺的猬 2020-12-06 14:52

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

p         


        
相关标签:
6条回答
  • 2020-12-06 15:31

    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.

    0 讨论(0)
  • 2020-12-06 15:35

    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.

    0 讨论(0)
  • 2020-12-06 15:36

    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);
        }
    });
    
    0 讨论(0)
  • 2020-12-06 15:47

    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");
    
    0 讨论(0)
  • 2020-12-06 15:47

    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!

    0 讨论(0)
  • 2020-12-06 15:51

    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.

    0 讨论(0)
提交回复
热议问题