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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:22:59+00:00 2026-06-15T15:22:59+00:00

The Java ExecutorService framework allows you to delegate a number of tasks to be

  • 0

The Java ExecutorService framework allows you to delegate a number of tasks to be performed using a managed thread pool so that N tasks can be performed X tasks at a time until complete.

My question is … what if N is a number that is either infinite or so large as to be impractical to allocate/assign/define initially.

How can you leverage the concept of thread pooling in Java (ExecutorService) to handle more tasks than you could reasonably submit without exhausting resources.

For purposes of this answer, assume each task is self-contained and does not depend on any other task and that tasks can be completed in arbitrary order.

My initial attempt at attacking this problem involved feeding the ExecutorService Y threads at a time but I quickly realized that there’s no apparent way to tell when a particular task is complete and therefore submit a new task to be executed.

I know I could write my own “ExecutorService” but I am trying to leverage the bounty of what the Java framework already provides. I’m generally in the “don’t re-invent the wheel” category because greater minds than mine have already made investments for me.

Thanks in advance to anybody that can provide any insight in how to attack this type of problem.

  • 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-15T15:23:00+00:00Added an answer on June 15, 2026 at 3:23 pm

    You could use a CompletionService to do it. You can have one thread that seeds the service with a bunch of tasks, then as tasks complete you can add new ones.

    A simple example:

    final CompletionService service = new ExecutorCompletionService(Executors.newFixedThreadPool(5));
    Runnable taskGenerator = new Runnable() {
        public void run() {
            // Seed the service
            for (int i = 0; i < 100; ++i) {
                service.submit(createNewTask());
            }
            // As tasks complete create new ones
            while (true) {
                Future<Something> result = service.take();
                processResult(result.get());
                service.submit(createNewTask());
            }
        }
    };
    new Thread(taskGenerator).start();
    

    This uses a ThreadPoolExecutor with 5 threads to process tasks and a hand-rolled producer/consumer thread for generating tasks and processing results.

    Obviously you’ll want something a little smarter than while (true), you’ll need to have sensible implementations of processResult and createNewTask, and this assumes that task execution is much slower than generating them or processing the results.

    Hopefully this will get you on the right track.

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

Sidebar

Related Questions

I'm using java.util.concurrent.ExecutorService with fixed thread pool to execute list of tasks. My list
I am using java.util.concurrent.ExecutorService.submit(Callable<T>) to invoke Callable tasks that return void. The reason that
I am using an unbounded, thread-caching Java-ExecutorService which concurrently processes my tasks. Now I
I'm using a java.util.concurrent.ExecutorService that I obtained by calling Executors.newSingleThreadExecutor() . This ExecutorService can
I'm looking for an ExecutorService implementation that can be provided with a timeout. Tasks
If I create a fixed size thread pool with 10 threads in java using
One of actions in Struts2 is using below code: java.util.concurrent.ExecutorService myservice = Executors.newSingleThreadExecutor(); myservice.execute(new
When I create ExecutorService with below code in JAVA,can someone explain how the ExecutorService
Possible Duplicate: Handling exceptions from Java ExecutorService tasks I use the ExecutorService from Java
Im using the ExecutorService in Java to invoke Threads with invokeAll() . After, I

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.