Java Shutdown挂钩未运行 [英] Java Shutdown hook not run

查看:113
本文介绍了Java Shutdown挂钩未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java / threads的新手,我继承了类似下面的代码。它是一个命令行程序,main()只启动5-6种不同类型的线程,并以^ C退出。我想添加一个关闭钩子来正确关闭所有线程并按以下方式调整它。



我在所有线程中添加了一个Shutdown钩子和一个stopThread()方法(比如MyWorker类中的那个)



<问题是,当我按^ CI时,看不到Thread的run方法中的结束消息。这是在后台完成还是我的方法有问题。另外,我应该遵循更好的模式吗?



谢谢

  public class Main {
public static MyWorker worker1 = new MyWorker();
// ..这里有各种其他线程

public static void startThreads(){
worker1.start();
// ..启动其他线程
}

public static void stopThreads(){
worker1.stopThread();
// ..停止其他线程
}

public static void main(String [] args)
throws Exception {

startThreads ();

// TODO这需要更多工作(稍后)

Runtime.getRuntime()。addShutdownHook(new Thread(){
@Override
public void run(){
try {
stopThreads();
} catch(Exception exp){

}
}
}) ;
}}

公共类MyWorker扩展Thread {
private volatile boolean stop = false;

public void stopThread(){
stop = true;
}

public void run(){
while(!stop){
//这里的东西
}
//打印退出消息与记录器
}
}


解决方案

当您调用System.exit()或通过信号终止时,它会停止所有现有线程并启动所有关闭挂钩。也就是说,当你挂机开始时你的所有线程都可能已经死了。



你应该确保干净地关闭资源,而不是试图干净地关闭线程。


I am new to Java/threads and I inherited something like the following code. It is a command line program that main() only starts 5-6 different kind of threads and exits with ^C. I want to add a shutdown hook to close all threads properly and adapted it the following way.

I added a Shutdown hook and a stopThread() method in all threads (like the one in MyWorker class)

The problem is that when I press ^C I don't see the end message from the Thread's run method. Is this done in the background or is there something wrong with my method. Also, Is there a better pattern I should follow?

Thanks

 public class Main {
     public static MyWorker worker1 = new MyWorker();
     // .. various other threads here

     public static void startThreads() {
         worker1.start();
         // .. start other threads
     }

     public static void stopThreads() {
         worker1.stopThread();
         // .. stop other threads
     }

     public static void main(String[] args)
             throws Exception {

         startThreads();

         // TODO this needs more work (later)

         Runtime.getRuntime().addShutdownHook(new Thread() {
             @Override
             public void run() {
                 try {
                     stopThreads();
                 } catch (Exception exp) {

                 }
             }
         });
     } }

 public class MyWorker extends Thread {
     private volatile boolean stop = false;

     public void stopThread() {
         stop = true;
     }

     public void run() {
         while (!stop) {
             // Do stuff here
         }
         // Print exit message with logger
     } 
}

解决方案

When you call System.exit() or terminate via a signal, it stop all the existing threads and starts all the shutdown hooks. i.e. all your threads could be dead by the time you hook starts.

Instead of trying to stop threads cleanly, you should ensure resources are closed cleanly.

这篇关于Java Shutdown挂钩未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆