“Closing” a blocking queue

前端 未结 10 697
不思量自难忘°
不思量自难忘° 2021-01-30 09:13

I’m using java.util.concurrent.BlockingQueue in a very simple producer-consumer scenario. E.g. this pseudo code depicts the consumer part:

class QueueCo         


        
10条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 09:51

    If you have a handle to the consumer thread, you can interrupt it. With the code you gave, that will kill the consumer. I would not expect the producer to have this; it would probably have to callback to the program controller somehow to let it know it's done. Then the controller would interrupt the consumer thread.

    You can always finish doing work before obeying the interrupt. For instance:

    class QueueConsumer implements Runnable {
        @Override
        public void run() {
            while(!(Thread.currentThread().isInterrupted())) {
                try {
                    final ComplexObject complexObject = myBlockingQueue.take();
                    this.process(complexObject);
    
                } catch (InterruptedException e) {
                    // Set interrupted flag.
                    Thread.currentThread().interrupt();
                }
            }
    
            // Thread is getting ready to die, but first,
            // drain remaining elements on the queue and process them.
            final LinkedList remainingObjects;
            myBlockingQueue.drainTo(remainingObjects);
            for(ComplexObject complexObject : remainingObjects) {
                this.process(complexObject);
            }
        }
    
        private void process(final ComplexObject complexObject) {
            // Do something with the complex object.
        }
    }
    

    I would actually prefer that to somehow poisoning the queue anyway. If you want to kill the thread, ask the thread to kill itself.

    (It's nice to see someone handling InterruptedException properly.)


    There seems to be some contention about the handling of interruptions here. First, I would like everyone to read this article: http://www.ibm.com/developerworks/java/library/j-jtp05236.html

    Now, with the understanding that no one actually read that, here's the deal. A thread will only receive an InterruptedException if it was currently blocking at the time of interrupt. In this case, Thread.interrupted() will return false. If it was not blocking, it will NOT receive this exception, and instead Thread.interrupted() will return true. Therefore, your loop guard should absolutely, no matter what, check Thread.interrupted(), or otherwise risk missing an interruption to the thread.

    So, since you are checking Thread.interrupted() no matter what, and you are forced to catch InterruptedException (and should be dealing with it even if you weren't forced to), you now have two code areas which handle the same event, thread interruption. One way to handle this is normalize them into one condition, meaning either the boolean state check can throw the exception, or the exception can set the boolean state. I choose the later.


    Edit: Note that the static Thread#interrupted method clears the the interrupted status of the current thread.

提交回复
热议问题