简体   繁体   English

如何杀死从第三方库导入的线程?

[英]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.

相关问题 如何模拟来自第三方库的类的静态调用 - How to mock a static call on a class from a third-party library 在哪里可以找到Spring导入的第三方jar代码 - Where to find the third-party jars imported by spring source code 如何调试第三方Gradle插件? - How to debug a third-party Gradle plugin? 如何测试仅使用第三方库中的操作的Callable方法? - How to test Callable method which uses only operations from third-party library? 如何防止classpath上的第三方JAR覆盖我的类依赖关系? - How to prevent third-party JARs on the classpath from overriding my class dependencies? Android:如何从代码中禁用所有第三方应用程序的通知声音? - Android: How to disable the sound of all third-party apps' notifications from code? 与第三方库分离的设计模式 - Design pattern to decouple from third party lib 如何确定从Java应用程序访问某些第三方程序和资源的方式? - How can I determine the way to access certain third-party programs and resources from my Java app? 从Eclipse中的第三方jar查看源代码 - Viewing source code from a third-party jar in Eclipse 在Java上从第三方HTML生成PDF - Generating PDF from a third-party HTML on java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM