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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:48:31+00:00 2026-05-21T19:48:31+00:00

I have a system that reads names from a list, calls an external server

  • 0

I have a system that reads names from a list, calls an external server for a true/false status check and actions those with a true status. the call to the external server take some time so running it all in one thread isn’t very efficient.

I am currently trying to implement it as a producer/consumer system where many consumer threads read the names from a list, call the external server, put the valid names in a blocking queue and have a single consumer pick items from the queue and action them. sadly however the system will at times run to completion and will at other times hang indefinitely.
Test code is as follows

public class SubscriberTest {
    static Queue<String> subscribed = new ConcurrentLinkedQueue<String>();
    static BlockingQueue<String> valid = new LinkedBlockingQueue<String>(100);
    Random rand = new Random();

    public SubscriberTest(int i) {
        for (int j = 0; j < i; j++) {
            subscribed.add("I love:" + j);
        }
    }

    public SubscriberTest(Queue<String> subs) {
        subscribed = subs;
    }

    public static void main(String[] args) {
        SubscriberTest fun = new SubscriberTest(10000);
        System.out.println(subscribed.size());
        ExecutorService producers = Executors.newCachedThreadPool();
        ExecutorService consumers = Executors.newSingleThreadExecutor();
        Consumer consumer = fun.new Consumer();
        Producer producer = fun.new Producer();
        while (!subscribed.isEmpty()) {
            producers.execute(producer);
            consumers.execute(consumer);
        }
        producers.shutdown();
        consumers.shutdown();
        System.out.println("finally");
    }

    // take names from subscribed and get status
    class Producer implements Runnable {
        public void run() {

            String x = subscribed.poll();
            System.out.println("Producer: " + x + " " + Thread.currentThread().getName());
            try {
                if (getStatus(x)) {
                    valid.put(x);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // this is a call to an external server
        private boolean getStatus(String x) {
            return rand.nextBoolean();
        }
    }

    // takes names from valid queue and save them
    class Consumer implements Runnable {

        public void run() {
            try {
                System.out.println("Consumer: " + valid.take() + " " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }
}

Please show me where I go wrong.

  • 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-21T19:48:32+00:00Added an answer on May 21, 2026 at 7:48 pm

    An ExecutorService is a pool of threads with a queue of tasks. Adding another queue just adds complexity and increases the chance you will do something incorrect. I suggest you just use the queue already there.

    public class SubscriberTest {
        public static void main(String[] args) throws InterruptedException {
            final ExecutorService consumers = Executors.newSingleThreadExecutor();
            // middle producer
        final ExecutorService producers = Executors.newFixedThreadPool(
                                       Runtime.getRuntime().availableProcessors());
        // subscribed/original producer.
        for (int i = 0; i < 1000*1000; i++) {
                final String task = "I love:" + i;
                producers.execute(new MidProducer(task, consumers));
            }
    
            producers.shutdown();
            producers.awaitTermination(10, TimeUnit.SECONDS);
            consumers.shutdown();
            System.out.println("finally");
        }
    
    
        static class MidProducer implements Runnable {
            private final Random rand = new Random();
            private final String task;
            private final ExecutorService consumers;
    
            public MidProducer(String task, ExecutorService consumers) {
                this.task = task;
                this.consumers = consumers;
            }
    
            public void run() {
                System.out.println("Producer: " + task + " " + Thread.currentThread().getName());
    
                if (getStatus(task))
                    consumers.execute(new Consumer(task));
            }
    
            private boolean getStatus(String x) {
                return rand.nextBoolean();
            }
    
        }
    
        static class Consumer implements Runnable {
            private final String task;
    
            private Consumer(String task) {
                this.task = task;
            }
    
            public void run() {
                System.out.println("Consumer: " + task + " " + Thread.currentThread().getName());
            }
        }
    }
    

    prints

    Producer: I love: 1 pool-2-thread-2
    Producer: I love: 3 pool-2-thread-4
    Producer: I love: 2 pool-2-thread-3
    Producer: I love: 5 pool-2-thread-2
    Producer: I love: 7 pool-2-thread-2
    Producer: I love: 4 pool-2-thread-5
    Producer: I love: 6 pool-2-thread-6
    Producer: I love: 8 pool-2-thread-7
    Producer: I love: 10 pool-2-thread-2
    Producer: I love: 9 pool-2-thread-5
    Producer: I love: 11 pool-2-thread-8
    Producer: I love: 12 pool-2-thread-9
    Producer: I love: 14 pool-2-thread-10
    Producer: I love: 13 pool-2-thread-2
    Producer: I love: 16 pool-2-thread-10
    Producer: I love: 15 pool-2-thread-11
    Producer: I love: 17 pool-2-thread-12
    Producer: I love: 20 pool-2-thread-14
    Producer: I love: 19 pool-2-thread-10
    Producer: I love: 18 pool-2-thread-13
    Producer: I love: 0 pool-2-thread-1
    Producer: I love: 22 pool-2-thread-12
    Producer: I love: 21 pool-2-thread-15
    Producer: I love: 25 pool-2-thread-3
    Producer: I love: 27 pool-2-thread-12
    Producer: I love: 26 pool-2-thread-10
    Producer: I love: 24 pool-2-thread-15
    Producer: I love: 28 pool-2-thread-1
    Producer: I love: 23 pool-2-thread-16
    Producer: I love: 31 pool-2-thread-11
    Producer: I love: 30 pool-2-thread-16
    Producer: I love: 32 pool-2-thread-1
    Producer: I love: 36 pool-2-thread-3
    Consumer: I love: 2 pool-1-thread-1
    

    …

    Consumer: I love: 9975 pool-1-thread-1
    Consumer: I love: 9977 pool-1-thread-1
    Consumer: I love: 9978 pool-1-thread-1
    Consumer: I love: 9979 pool-1-thread-1
    Consumer: I love: 9981 pool-1-thread-1
    Producer: I love: 9996 pool-2-thread-16
    Consumer: I love: 9984 pool-1-thread-1
    Consumer: I love: 9985 pool-1-thread-1
    Consumer: I love: 9990 pool-1-thread-1
    Consumer: I love: 9992 pool-1-thread-1
    Producer: I love: 9997 pool-2-thread-16
    Consumer: I love: 9994 pool-1-thread-1
    Consumer: I love: 9995 pool-1-thread-1
    Consumer: I love: 9996 pool-1-thread-1
    Producer: I love: 9998 pool-2-thread-16
    Producer: I love: 9999 pool-2-thread-16
    Consumer: I love: 9997 pool-1-thread-1
    Consumer: I love: 9998 pool-1-thread-1
    Consumer: I love: 9999 pool-1-thread-1
    finally
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At my current job we have a CMS system that is .NET/SQL Server based.
I have a list of Customer objects that I need to have selectable from
I have a system that combines the best and worst of Java and PHP.
I have a system that uses a meta refresh to a logout page, which
I have a system that creates an order and that order can be billed
We have a system that has a Mysql database with about 2Gigs of data.
Here's an interesting question. I have a system that attempts to run some initialization
I have a shop system that integrates PayPal in the usual way, i.e. the
I have an old system that uses XML for it's data storage. I'm going
The scenario: we have a web system that automatically generates office 2003 excel files

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.