问题

我需要一个解决方案来正确地停止Java中的线程。

我有IndexProcessorclass,它实现了Runnable接口:

public class IndexProcessor implements Runnable {

    private static final Logger LOGGER = LoggerFactory.getLogger(IndexProcessor.class);

    @Override
    public void run() {
        boolean run = true;
        while (run) {
            try {
                LOGGER.debug("Sleeping...");
                Thread.sleep((long) 15000);

                LOGGER.debug("Processing");
            } catch (InterruptedException e) {
                LOGGER.error("Exception", e);
                run = false;
            }
        }

    }
}

我有启动和停止线程的ServletContextListener类:

public class SearchEngineContextListener implements ServletContextListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(SearchEngineContextListener.class);

    private Thread thread = null;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        thread = new Thread(new IndexProcessor());
        LOGGER.debug("Starting thread: " + thread);
        thread.start();
        LOGGER.debug("Background process successfully started.");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        LOGGER.debug("Stopping thread: " + thread);
        if (thread != null) {
            thread.interrupt();
            LOGGER.debug("Thread successfully stopped.");
        }
    }
}

但是当我关闭tomcat时,我在IndexProcessor类中得到了异常:

2012-06-09 17:04:50,671 [Thread-3] ERROR  IndexProcessor Exception
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at lt.ccl.searchengine.processor.IndexProcessor.run(IndexProcessor.java:22)
    at java.lang.Thread.run(Unknown Source)

我使用的是JDK 1.6。所以问题是:

如何停止线程而不抛出任何异常?

**P.S.**我不想使用.stop();方法,因为它已被弃用。


#1 热门回答(258 赞)

使用Thread.interrupt()是一种完全可以接受的方式。事实上,如上所述,它可能更适合于旗帜。原因是,如果你处于可中断的阻塞调用(如Thread.sleep或使用java.nio频道操作),你实际上可以立即突破这些。

如果使用标志,则必须等待阻止操作完成,然后才能检查标志。在某些情况下,你无论如何都必须这样做,例如使用不可中断的标准InputStream/OutputStream

在这种情况下,当线程被中断时,它不会中断IO,但是,你可以在代码中轻松地执行此操作(并且你应该在可以安全停止和清理的关键点处执行此操作)

if (Thread.currentThread().isInterrupted()) {
  // cleanup and stop execution
  // for example a break in a loop
}

就像我说的那样,Thread.interrupt()的主要优点是你可以立即打破可中断的调用,这是你无法用标志方法做的。


#2 热门回答(145 赞)

IndexProcessorclass中,你需要一种设置标志的方法,该标志通知线程它将需要终止,类似于你在类范围中使用的变量run

当你想要停止线程时,你设置这个标志并在线程上调用join()并等待它完成。

通过使用volatile变量或使用与用作标志的变量同步的getter和setter方法,确保该标志是线程安全的。

public class IndexProcessor implements Runnable {

    private static final Logger LOGGER = LoggerFactory.getLogger(IndexProcessor.class);
    private volatile boolean running = true;

    public void terminate() {
        running = false;
    }

    @Override
    public void run() {
        while (running) {
            try {
                LOGGER.debug("Sleeping...");
                Thread.sleep((long) 15000);

                LOGGER.debug("Processing");
            } catch (InterruptedException e) {
                LOGGER.error("Exception", e);
                running = false;
            }
        }

    }
}

然后在SearchEngineContextListener

public class SearchEngineContextListener implements ServletContextListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(SearchEngineContextListener.class);

    private Thread thread = null;
    private IndexProcessor runnable = null;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        runnable = new IndexProcessor();
        thread = new Thread(runnable);
        LOGGER.debug("Starting thread: " + thread);
        thread.start();
        LOGGER.debug("Background process successfully started.");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        LOGGER.debug("Stopping thread: " + thread);
        if (thread != null) {
            runnable.terminate();
            thread.join();
            LOGGER.debug("Thread successfully stopped.");
        }
    }
}

#3 热门回答(20 赞)

简单回答:你可以通过以下两种常见方式之一内部停止线程:

  • run方法命中一个return子例程。
  • 运行方法完成,并隐式返回。

你还可以外部停止线程:

  • 调用system.exit(这会终止你的整个过程)
  • 调用线程对象的interrupt()方法*
  • 看看线程是否有一个听起来像它可以工作的实现方法(如kill()或stop())

*:期望是这应该停止一个线程。但是,当发生这种情况时,线程实际执行的操作完全取决于开发人员在创建线程实现时所写的内容。

你在run方法实现中看到的常见模式是awhile(boolean){},其中布尔值通常名为isRunning,它是其线程类的成员变量,它是易失性的,并且通常可以通过其他类型的setter方法访问,例如,kill() { isRunnable=false; }。这些子例程很好,因为它们允许线程在终止之前释放它所拥有的任何资源。


原文链接