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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:10:59+00:00 2026-05-23T23:10:59+00:00

I converted a working Producer/Consumer Example from Thread/Runnable to Executor/Callable/BlockingQueues and using the Poison

  • 0

I converted a working Producer/Consumer Example from Thread/Runnable to Executor/Callable/BlockingQueues and using the Poison Pill termination pattern.

If you run the program below, it will hang for few minutes even though every thread has completed.
jstack shows numerous threads blocked on a queue that is not seemingly related to the application.

"pool-1-thread-10" prio=5 tid=10b08d000 nid=0x10d91c000 waiting on condition [10d91b000]
   java.lang.Thread.State: TIMED_WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <7f3113510> (a java.util.concurrent.SynchronousQueue$TransferStack)
    at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:198)
    at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:424)
    at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:323)
    at java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:874)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:945)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:680)

I can not figure out why the application hangs. Any help is appreciated.
Thank you

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;

public class ProducersConsumers {
    private LinkedBlockingQueue<Item> queue = new LinkedBlockingQueue<Item>();
    private static final ExecutorService executorPool = Executors.newCachedThreadPool();
    private Random randGenerator = new Random(System.currentTimeMillis());

    private class Item {
        private boolean done = false;
        private String message;

        private Item(boolean done) {
            this.done = done;
        }

        private Item(String message) {
            this.message = message;
        }

        public boolean isDone() {
            return done;
        }

        public String getMessage() {
            return message;
        }
    }

    private class Producer implements Callable<Long> {
        private final int id;
        private Integer numOfMessages;

        private Producer(int id, int numOfMessages) {
            this.id = id;
            this.numOfMessages = numOfMessages;
        }

        @Override
        public Long call() throws Exception {
            long totalTime = 0;
            while (numOfMessages > 0) {
                String message;
                synchronized (numOfMessages) {
                    long starttime = System.nanoTime();
                    int msgLength = randGenerator.nextInt(20000);
                    StringBuilder sb = new StringBuilder(msgLength);
                    for (int a = 0; a < msgLength; a++) {
                        sb.append((char) ('a' + randGenerator.nextInt(26)));
                    }
                    message = sb.toString();
                    long endtime = System.nanoTime();
                    totalTime += endtime - starttime;
                }
                numOfMessages--;
                queue.put(new Item(message));
            }
            System.out.println("-------------Producer " + id + " is done.");
            queue.put(new Item(true));
            return totalTime;
        }
    }

    private class Consumer implements Callable<Long> {
        private String monitor = "monitor";
        private final int id;

        private Consumer(int id) {
            this.id = id;
        }

        @Override
        public Long call() throws Exception {
            long totalTime = 0;
            while (true) {
                Item item = queue.take();
                if (item.isDone()) {
                    break;
                }
                synchronized (monitor) {
                    long starttime = System.nanoTime();
                    StringBuilder sb = new StringBuilder(item.getMessage());
                    sb = sb.reverse();
                    String message = sb.toString();
                    long endtime = System.nanoTime();
                    totalTime += endtime - starttime;
                }
            }
            System.out.println("+++++++++++++Consumer " + id + " is done.");
            return totalTime;
        }
    }

    public void begin(int threadCount) throws InterruptedException, ExecutionException {
        Collection<Producer> producers = new ArrayList<Producer>();
        for (int i = 0; i < threadCount; i++) {
            producers.add(new Producer(i, randGenerator.nextInt(5)));
        }
        Collection<Consumer> consumers = new ArrayList<Consumer>();
        for (int i = 0; i < threadCount; i++) {
            consumers.add(new Consumer(i));
        }
        try {
            long starttime = System.nanoTime();
            List<Future<Long>> producerFutureList = executorPool.invokeAll(producers);
            List<Future<Long>> consumerFutureList = executorPool.invokeAll(consumers);
            long producerTotalTime = 0;
            long consumerTotalTime = 0;

            for (Future<Long> future : producerFutureList) {
                producerTotalTime += future.get();
            }
            for (Future<Long> future : consumerFutureList) {
                consumerTotalTime += future.get();
            }
            long mainThreadTotalTime = System.nanoTime() - starttime;

            System.out.println("producerTotalTime   " + producerTotalTime);
            System.out.println("consumerTotalTime   " + consumerTotalTime);
            System.out.println("mainThreadTotalTime " + mainThreadTotalTime);
            System.out.println("Difference          " + (producerTotalTime + consumerTotalTime - mainThreadTotalTime));
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            throw e;
        } catch (ExecutionException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            throw e;
        }

    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ProducersConsumers prodcon = new ProducersConsumers();
        prodcon.begin(20);
    }
}
  • 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-23T23:10:59+00:00Added an answer on May 23, 2026 at 11:10 pm

    You should close the ExecutorService when you are done with it. Call executorPool.shutdown() at the end of your program.

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

Sidebar

Related Questions

I am working with a set of data that I have converted to a
I am working on eCommerce app Using Asp.net 3.5 MVC. On my Cart View
I just got awesome_nested_set put in place and all is working well. I converted
Right now I'm working on a project which requires an integer to be converted
My Scenario I'm working on a database which will contain many details from various
I've converted a bunch of DML (INSERT/UPDATE/DELETE) queries from Oracle into PostgreSQL and now
I am working in Java, inserting data from HashMaps of certain types into a
Background I am working on a phonetic converter program which converts english text into
I'm working on a project to convert an existing Java web application to use
I am working on a tool where I need to convert string values to

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.