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 8348587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:44:10+00:00 2026-06-09T07:44:10+00:00

Yes, this is yet another question on producer/consumer in Java. My variant is that

  • 0

Yes, this is yet another question on producer/consumer in Java.

My variant is that I have N producers started by the produceDataRows method and M consumers started by the consumeDataRows method.
Both methods start their own instance of the ThreadPoolExecutor class, submit the respective number of producer/consumer tasks and then wait until their executors complete.

So, here is my code:

final BlockingQueue<Row> allRows = new LinkedBlockingQueue<Row>();
ExecutorService exec = Executors.newFixedThreadPool(2);
FutureTask<Object> producer = new FutureTask<Object>(new Callable<Object>() {
  @Override
  public Object call() throws Exception {
    produceDataRows(allRows);
    return null;
  }
});
FutureTask<Object> consumer = new FutureTask<Object>(new Callable<Object>() {
  @Override
  public Object call() throws Exception {
    consumeDataRows(allRows);
    return null;
  }
});
exec.execute(producer);
exec.execute(consumer);
producer.get();
consumer.get();

The problem is that consumer.get() returns, but consumeDataRows is never called. The produceDataRows, on the other hand is called.

What am I missing?

Thanks.

EDIT 1

Following the reply by Gray, I have rewritten the code as follows:

ExecutorService exec = Executors.newFixedThreadPool(2);
Callable<Object> producer = new Callable<Object>() {
  @Override
  public Object call() throws Exception {
    produceDataRows(allRows);
    return null;
  }
};
Callable<Object> consumer = new Callable<Object>() {
  @Override
  public Object call() throws Exception {
    consumeDataRows(allRows);
    return null;
  }
};
exec.submit(producer);
exec.submit(consumer);
exec.shutdown();
exec.awaitTermination(10, TimeUnit.DAYS);

The same effect – the code terminates, but consumeDataRows is never called. I have three different ThreadPoolExecutor instances here – one in produceDataRows, one in consumeDataRows and the last one here.

Thanks.

EDIT 2

There is something wrong with my produceDataRows method, because if I comment it out, then execution visits the call method of both callables. Trying to figure it out now.

  • 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-06-09T07:44:12+00:00Added an answer on June 9, 2026 at 7:44 am

    Edit:

    I think you have a race condition here. Since the producer and the consumer are both running at the same time, I bet the consumer goes to see what rows are in allRows and gets none because the producer has not done anything yet.

    You will need to run the producer ahead of time or have the consumer use allRows.poll(long timeout, TimeUnit unit) to wait for the results of the producer. Maybe the producer could set a volatile boolean producedAll = true once it had produced all of the the rows and completed, and have the consumer looping with .poll(...) waiting for the producedAll to be true.


    You have your classes a bit confused. You should be calling exec.submit(...) on your Callable not instantiating and executing a FutureTask. submit(...) then returns a Future which you can call get() on.

    What you are doing now is instantiating your own FutureTask which is done internally by the ExecutorService — never by the caller. When you are then calling get() on your FutureTask, it is just returning immediately and not doing any joining or anything. It works (unfortunately) because FutureTask is a Runnable.

    Your code should be doing something like:

    Future<Object> producer = exec.submit(new Callable<Object>() {
        @Override
        public Object call() throws Exception {
           produceDataRows(allRows);
           return null;
        }
    });
    Future<Object> consumer = exec.submit(new Callable<Object>() {
        ...
    });
    producer.get();
    consumer.get();
    

    Btw, typically I use Callable<Void> if I don’t care about the return from a Callable since I just want the Exception. I still return null but Void is useful when you want a no-value object.

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

Sidebar

Related Questions

Yes, this is probably yet another greatest-n-per-group question... But I've tried at least a
Yes, I know, yet another question about mutable objects. See this for general background
Yet another Lucene.net question by an extreme newbie to it. This time, I have
Yes, I'm yet another person to as this annoying question... but I'm not convinced
I have three code snippets. This one: 1,7; //yes, that's all the code compiles
yes this is an iOS programming question. I need to calculate the exact time
This question falls into the yes - this works, yes - this is ugly,
(Before anyone says anything Yes this was homework but i have already turned it
I know the Sales pitch answer is yes to this question, but is it
Yes, this is a programming-related question, if a little indirectly. For better or worse,

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.