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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:01:01+00:00 2026-05-20T23:01:01+00:00

I have recursive code that processes a tree structure in a depth first manner.

  • 0

I have recursive code that processes a tree structure in a depth first manner. The code basically looks like this:

function(TreeNode curr) 
{
    if (curr.children != null && !curr.children.isEmpty()) 
    {
        for (TreeNode n : curr.children) 
    {
            //do some stuff
            function(n);
        }
    } 
    else 
    {
        //do some other processing
    }
}

I want to use threads to make this complete faster. Most of the time is spent traversing so I don’t want to just create a thread to handle “the other processing” because it doesn’t take that long. I think I want to fork threads at “do some stuff” but how would that work?

  • 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-20T23:01:02+00:00Added an answer on May 20, 2026 at 11:01 pm

    It’s a good case for Fork/Join framework which is to be included into Java 7. As a standalone library for use with Java 6 it can be downloaded here.

    Something like this:

    public class TreeTask extends RecursiveAction {
        private final TreeNode node;
        private final int level;
    
        public TreeTask(TreeNode node, int level) {
            this.node = node;
            this.level = leve;
        }
    
        public void compute() {
            // It makes sense to switch to single-threaded execution after some threshold
            if (level > THRESHOLD) function(node);
    
            if (node.children != null && !node.children.isEmpty()) {
                List<TreeTask> subtasks = new ArrayList<TreeTask>(node.children.size());
                for (TreeNode n : node.children) {
                    // do some stuff
                    subtasks.add(new TreeTask(n, level + 1));
                }
                invokeAll(subtasks); // Invoke and wait for completion
            } else {
                //do some other processing
            }
        }
    }
    
    ...
    ForkJoinPool p = new ForkJoinPool(N_THREADS);
    p.invoke(root, 0);
    

    The key point of fork/join framework is work stealing – while waiting for completion of subtasks thread executes other tasks. It allows you to write algorithm in straightforward way, while avoiding problems with thread exhausting as a naive apporach with ExecutorService would have.

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

Sidebar

Related Questions

I have this recursive code that transverses the DOM and adds a prefix to
I have some code that does something like this: abstract class Data { Data(string
i have this recursive code <ul> <% sql=SELECT * FROM cats ORDER BY orderid
How can I speed up a recursive code using OpenMP? Basically, I have to
i have a recursive query like this (note: this is just an example): var
I have a recursive model like this: public class Node { public int Id
I have made this code that makes some visual tiles that fades in and
I have the following recursive code and it doesn't behave as expected (see details
I have a recursive function working on a list, the function contains a loop
I have a recursive method on the base form that takes in a control

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.