Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4103294
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:56:23+00:00 2026-05-20T20:56:23+00:00

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

  • 0

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

class QueueConsumer implements Runnable {

    @Override
    public void run() {
        while(true)
        {
            try {
                ComplexObject complexObject = myBlockingQueue.take();
                //do something with the complex object
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

So far so good. In the javadoc of the blocking queue I read:

A BlockingQueue does not intrinsically
support any kind of “close” or
“shutdown” operation to indicate that
no more items will be added. The needs
and usage of such features tend to be
implementation-dependent. For example,
a common tactic is for producers to
insert special end-of-stream or poison
objects, that are interpreted
accordingly when taken by consumers.

Unfortunately because of the generics in use and the nature of ComplexObject it’s not trivial to push a “poison object” into the queue. So this “common tactic” is not really convenient in my scenario.

My question is: what other good tactics/patterns can I use to “close” the queue?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:56:24+00:00Added an answer on May 20, 2026 at 8:56 pm

    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<ComplexObject> 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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a java.util.concurrent.ExecutorService that I obtained by calling Executors.newSingleThreadExecutor() . This ExecutorService can
Is there any advantage of using java.util.concurrent.CountdownLatch instead of java.util.concurrent.Semaphore ? As far as
I have code where I schedule a task using java.util.Timer . I was looking
Using java, minus the exception handling, it is as simple as FileOutputStream ostream =
I am using Java 1.4 with Log4J. Some of my code involves serializing and
I'm using Java, and need to generate a simple, standards-compliant RSS feed. How can
Using Java, how can I test that a URL is contactable, and returns a
I am using Java back end for creating an XML string which is passed
I'm using Java 6, Tomcat 6, and Metro. I use WebService and WebMethod annotations
I've just started using Java's enums in my own projects (I have to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.