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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:02:32+00:00 2026-05-20T15:02:32+00:00

We had a bit of a problem. :) We want to ensure that only

  • 0

We had a bit of a problem. 🙂

We want to ensure that only N threads are doing background tasks at any time. To do this, we used a fixed thread pool executor. It seemed to be working fine.

Then we found an issue. Suppose you have a class which uses the executor to do some parallel work and then it calls some other class while in the executor thread which also does some parallel work, intending to wait on it. Here’s what happens:

  • Main thread calls the first level method.
  • This method thinks it can parallelise into 16 tasks and splits up its work.
  • 16 tasks are submitted to the executor.
  • Main thread starts waiting for its tasks to complete.
  • Supposing there are four threads available, the first four tasks each get picked up and run. So there are 12 tasks left on the queue.
  • Now, one of these tasks calls some other method.
  • This new method thinks it can parallelise into 2 tasks. Let’s say it’s the first step in a parallel merge sort or something along those lines.
  • 2 tasks are submitted to the executor.
  • This thread now starts waiting for its tasks to complete.

Uh-oh. So at this point, all four threads will now be waiting for tasks to complete but they are collaboratively blocking the executor actually running those tasks.

Solution 1 to this problem was as follows: on submitting a new task to the executor, if we are already running all our threads, and we are already running on one of the executor threads, run the task inline. This worked fine for 10 months, but now we have hit a problem with it. If the new tasks it is submitting are still relatively large, then you can get into a situation where the new task blocks the method from adding the other tasks to the queue, which would otherwise be able to be picked up by the other worker threads. So you get periods of huge delays while a thread is processing the work inline.

Is there a better solution to the core problem of executing a potentially unbounded tree of background tasks? I understand that .NET’s equivalent to the executor service has some kind of in-built ability to steal from the queue which prevents the original deadlock issue from occurring, which as far as I can tell is an ideal solution. But what about over in Java land?

  • 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-20T15:02:33+00:00Added an answer on May 20, 2026 at 3:02 pm

    Java 7 has the concept of a ForkJoinPool that allows a task to “fork” off another task by submitting it tot he same Executor. Then gives it the option of later attempting to “help join” that task by attempting to run it if it has not been run.

    I believe the same thing can be done in Java 6 by simple combining an Executor with FutureTask. Like so:

    public class Fib implements Callable<Integer> {
        int n;
        Executor exec;
    
        Fib(final int n, final Executor exec) {
            this.n = n;
            this.exec = exec;
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public Integer call() throws Exception {
            if (n == 0 || n == 1) {
                return n;
            }
    
            //Divide the problem
            final Fib n1 = new Fib(n - 1, exec);
            final Fib n2 = new Fib(n - 2, exec);
    
            //FutureTask only allows run to complete once
            final FutureTask<Integer> n2Task = new FutureTask<Integer>(n2);
            //Ask the Executor for help
            exec.execute(n2Task);
    
            //Do half the work ourselves
            final int partialResult = n1.call();
    
            //Do the other half of the work if the Executor hasn't
            n2Task.run();
    
            //Return the combined result
            return partialResult + n2Task.get();
        }
    
    }        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've had a bit of trouble finding any information on this and all the
I have run into a bit of a problem here: I had a problem-specific
For years, on 32-bit systems I have never had a problem. Why can't I
I've done quite a bit of googling on this error but have had no
I've done a bit of googling round but this particular problem is a little
Firstly I want to tell you a bit about the background. I've got a
Maybe is simple problem that I don't see, but is a bit tricky to
I have quite a bit of experience with css and even this problem has
i had a problem where i was trying to reconstruct the the formula used
This might seem a bit silly, but I want to try and solve this

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.