[英]How to kill a thread imported from third-party lib?
I import a third-party lib in my project, and now we publish it on Websphere(I use a ServletContextListener to clean all the treads in my application, use Thread.stop() method), but everytime we redeploy this app, I found the old thread still alive, I search on internet,and know it shoud use a voilate member or with interrupt() , but I don't wanna hack on third-party lib, so who can give me a hint? 我在项目中导入了第三方库,现在我们将其发布在Websphere上(我使用ServletContextListener清理应用程序中的所有踩踏,使用Thread.stop()方法),但是每次我们重新部署此应用程序时,我都发现旧线程仍然存在,我在Internet上搜索了一下,知道它应该使用voilate成员或与interrupt()一起使用 ,但是我不想破解第三方lib,所以谁能给我提示?
thanks:) 谢谢:)
third-party lib code as follow: 第三方库代码如下:
public void run() {
while (true) {
try {
for (DefaultFuture future : FUTURES.values()) {
if (future == null || future.isDone()) {
continue;
}
if (System.currentTimeMillis() - future.getStartTimestamp() > future.getTimeout()) {
// create exception response.
Response timeoutResponse = new Response(future.getId());
// set timeout status.
timeoutResponse.setStatus(future.isSent() ? Response.SERVER_TIMEOUT : Response.CLIENT_TIMEOUT);
timeoutResponse.setErrorMessage(future.getTimeoutMessage(true));
// handle response.
DefaultFuture.received(future.getChannel(), timeoutResponse);
}
}
Thread.sleep(30);
} catch (Throwable e) {
logger.error("Exception when scan the timeout invocation of remoting.", e);
}
}
}
I make a simple local test, and found thread.stop() can stop the thread, and use local jetty, I can reproduce the problem, who can explain it? 我做了一个简单的本地测试,发现thread.stop()可以停止线程,并使用本地码头,我可以重现该问题,谁能解释呢?
my local test code: 我的本地测试代码:
public class Test {
public static void main(String[] args) throws InterruptedException, IOException {
myThread t1 = new myThread();
t1.start();
Thread.sleep(4000);
t1.stop();
System.in.read();
}
} }
class myThread extends Thread{
@Override
public void run() {
int i=0;
while(true){
try {
System.out.println(i++);
Thread.sleep(30);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
} }
The stop method is deprecated. 不建议使用stop方法。 It is unsafe.
这是不安全的。 You should read the Oracle tutorial - Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.
您应该阅读Oracle教程- 为什么不赞成使用Thread.stop,Thread.suspend和Thread.resume?
Refer to the paragraph: 请参阅以下段落:
What should I use instead of Thread.stop?
我应该用什么代替Thread.stop? For example, suppose your applet contains the following start, stop and run methods:
例如,假设您的小程序包含以下start,stop和run方法:
private Thread blinker; public void start() { blinker = new Thread(this); blinker.start(); } public void stop() { blinker.stop(); // UNSAFE! } public void run() { while (true) { try { Thread.sleep(interval); } catch (InterruptedException e){ } repaint(); } }
You can avoid the use of Thread.stop by replacing the applet's stop and run methods with: private volatile Thread blinker;
您可以通过将applet的stop和run方法替换为以下方法来避免使用Thread.stop:
public void stop() { blinker = null; } public void run() { Thread thisThread = Thread.currentThread(); while (blinker == thisThread) { try { Thread.sleep(interval); } catch (InterruptedException e){ } repaint(); } }
Why don't you extend the third-party Class, and re-write the thread method? 为什么不扩展第三方Class,然后重新编写thread方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.