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

  • SEARCH
  • Home
  • 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 7413673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:56:34+00:00 2026-05-29T06:56:34+00:00

When working with a BlockingQueue , i implemented the following logic to read from

  • 0

When working with a BlockingQueue, i implemented the following logic to read from it until told otherwise. Unfortunately the following is happening, intermittently:

The problem:

  • Even after shouldContinueReading is set to false, loop does not CONSISTENTLY break
  • The problem is intermittent, sometimes everything works fine

As part of the QThread class, i declare:

public static volatile boolean          shouldContinueReading   = true;

Run (confirmed to be executing) method contains:

    while (shouldContinueReading) {
        try {

            String retrieved = qIn.poll(2, TimeUnit.MILLISECONDS);
            if (retrieved != null)
                consume(retrieved);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    System.out.println("I am out");  // <-- not always seen
    if (qIn.remainingCapacity() > 0) {
        try {
            consume(qIn.take());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

While this is going on, in another thread, when certain things happen, shouldContinueReading changes state

    while (stillReading) {
        // do nothing
    }

    QThread.shouldContinueReading = false;

Update: problem resolved

Turns out the problem lies a bit further:

private void consume(String take) {
            // some processing

    produce(newData.toString());
}

private void produce(String newData) {
    System.out.println(newData);
    try {
        qOut.put(newData);          // <-- Problem is here. Should use offer instead of put
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

Both qIn (queue in) and qOut (queue out) are declared as:

private volatile BlockingQueue<String>  qIn;
private volatile BlockingQueue<String>  qOut;

The objects themselves are created elsewhere as follows and passed down to the constructor:

    BlockingQueue<String> q1 = new SynchronousQueue<String>();
    BlockingQueue<String> q2 = new SynchronousQueue<String>();

    QThread qThread = new QThread(q1, q2);

Any suggestions? what i should do with qOut? Am i not declaring it correctly?

  • 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-29T06:56:35+00:00Added an answer on May 29, 2026 at 6:56 am

    I bet QThread.shouldContinueReading = false; isn’t getting executed always,or the reading thread is not executing in the first place. I.e. the problem you are seeing is likely somewhere up the stream — not here. The first thing I’d do would be to pin down where exactly the problem lies, with 100% confidence (put some more print statements).

    Apart from the problem, I’d recommend to use the thread interruption mechanism instead of rolling your own flag (which is, in turn just a glorified flag, but that way you can affect third party codes like BlockedQueue and make the implementation simpler and more efficient even) especially if this is production code.

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

Sidebar

Related Questions

Working with an Oracle 9i database from an ASP.NET 2.0 (VB) application using OLEDB.
Working on calling a C function from my asm project. I'm trying to push
Working sample using one Table SELECT t.* FROM ( SELECT TITLE.name, (TITLE.value-TITLE.msp) AS Lower,
Working SQL The following code works as expected, returning two columns of data (a
Working with COM interop, I can call a managed function from within unmanaged code.
I converted a working Producer/Consumer Example from Thread/Runnable to Executor/Callable/BlockingQueues and using the Poison
Working live URL showing problem: http://69.24.73.172/demos/newDemo/test.html The HTML : <div class="small-vote"> <a href="#" class="s
I have a blocking queue and workers taking from that queue and then working
Working with SQL server with isolation level read committed snapshot, we routinely write data
Working with C and Win32, I have a problem where my program freezes instead

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.