How to stop uninterruptible threads in Java

后端 未结 6 1959
有刺的猬
有刺的猬 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: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);
        }
    });
    

提交回复
热议问题