Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
734 views
in Technique[技术] by (71.8m points)

multithreading - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...