在Java中停止循环线程 [英] Stopping looping thread in Java

查看:151
本文介绍了在Java中停止循环线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个不断从队列中读取的线程。

I'm using a thread that is continuously reading from a queue.

类似于:

public void run() {
    Object obj;
    while(true) {
        synchronized(objectsQueue) {
            if(objectesQueue.isEmpty()) {
                try {
                    objectesQueue.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                obj = objectesQueue.poll();
            }
        }

        // Do something with the Object obj
    }
}

停止此线程的最佳方法是什么?

What is the best way to stop this thread?

我看到两个选项:

1 - 由于 Thread.stop()已弃用,我可以实现 stopThisThread()使用原子检查条件变量的方法。

1 - Since Thread.stop() is deprecated, I can implement a stopThisThread() method that uses a n atomic check-condition variable.

2 - 将死亡事件对象或类似的东西发送到队列。当线程获取死亡事件时,它会退出。

2 - Send a Death Event object or something like that to the queue. When the thread fetches a death event, it exits.

我更喜欢第一种方式,但是,我不知道何时调用 stopThisThread ()方法,因为它可能在队列中,并且停止信号可以首先到达(不可取)。

I prefer the 1st way, however, I don't know when to call the stopThisThread() method, as something might be on it's way to the queue and the stop signal can arrive first (not desirable).

任何建议?

推荐答案

DeathEvent (或者通常称之为如果您需要在关闭之前完成队列中的所有工作,那么毒丸)方法很有效。问题是这可能需要很长时间。

The DeathEvent (or as it is often call, "poison pill") approach works well if you need to complete all of the work on the queue before shutting down. The problem is that this could take a long time.

如果你想尽快停止,我建议你这样做

If you want to stop as soon as possible, I suggest you do this

BlockingQueue<O> queue = ...

...

public void run() {
   try {
       // The following test is necessary to get fast interrupts.  If
       // it is replaced with 'true', the queue will be drained before
       // the interrupt is noticed.  (Thanks Tim)
       while (!Thread.interrupted()) {
           O obj = queue.take();
           doSomething(obj);
       }
   } catch (InterruptedException ex) {
       // We are done.
   }
}

停止线程 t 使用 run 方法实例化,只需调用 t.interrupt();

To stop the thread t that instantiated with that run method, simply call t.interrupt();.

如果您将上述代码与其他答案进行比较,您会注意到如何使用 BlockingQueue Thread.interrupt()简化了解决方案。

If you compare the code above with other answers, you will notice how using a BlockingQueue and Thread.interrupt() simplifies the solution.

我还声称额外的停止标志是不必要的,从大局来看,可能有害。一个表现良好的工作线程应该尊重中断。意外中断只是意味着工作程序正在原始程序员没有预料到的上下文中运行。最好的事情是如果工人做了被告知要做的事情......即它应该停止......这是否符合原始程序员的概念。

I would also claim that an extra stop flag is unnecessary, and in the big picture, potentially harmful. A well-behaved worker thread should respect an interrupt. An unexpected interrupt simply means that the worker is being run in a context that the original programmer did not anticipate. The best thing is if the worker to does what it is told to do ... i.e. it should stop ... whether or not this fits with the original programmer's conception.

这篇关于在Java中停止循环线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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