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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:15:34+00:00 2026-06-14T07:15:34+00:00

I have a tree structure that I am trying to use a recursive method

  • 0

I have a tree structure that I am trying to use a recursive method to add, get, or remove an item. I would like to be able to introduce a new thread for every child node that I find with an iterator. I currently pass the node into the recursive method when I call it. I would like to be able to start a thread and tell that thread to call that method. How would I do something similar to saying new thread, thread.callMethod()? I need to have a Runnable interface and a run method apparently? However this seems to complicate things much more than necessary (I already have method names). Does anyone know a good way to do this using run(..), or does anyone know a better way to do this without using run(..). Thanks.

Thank you for your answers. I had been thinking that I need to somehoe get a new instance of a node running on a new thread, but that is not the case. The node just occupies space in memory and the thread is reference to the method code that is executed on the instance living in memory (my CS 302 TA is already disagreeing with me in my head). So.. I had been thinking about enums, but I was thinking that I might somehow need to have all my nodes running on separate threads during instantiation, or else, have the methods actually written in new classes that implement runnable.

In other words,

public class TreeMethods implements Runnable
{
...
    run(.. node, .. params, .. enum)
    {
        switch(enum)
        case(add)
        {
             myThreadInstanceMethod(node);
        }
    ...
    }
    myThreadInstanceMethod(..) {..}
}

Thanks. I didn’t know that I was asking this question, but you’ve just simplified my thread management design process greatly.

  • 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-14T07:15:36+00:00Added an answer on June 14, 2026 at 7:15 am

    You can’t do it without a Runnable or Callable object. The proper way to do what you want is to create a Runnable class that takes your object and calls the appropriate method on that object.

    public class MyRunnable implements Runnable {
        private MyObject obj;
        public MyRunnable(MyObject obj) {
           this.obj = obj;
        }
        public void run() {
           obj.someMethod();
        }
    }
    

    If you need to call one of a couple methods then you can use an enum for this. If you need to pass in arguments then you can add them to the constructor.

    With threads, it’s always recommended that you use the Executors class and the ExecutorService thread pools. This will limit the number of threads created by your recursive algorithm and keep the thread particulars hidden.

    // create a thread pool with 10 workers
    ExecutorService threadPool = Executors.newFixedThreadPool(10);
    // or you can create an open-ended thread pool
    // ExecutorService threadPool = Executors.newCachedThreadPool();
    while (recursing) {
        threadPool.submit(new MyRunnable(myObject));
    }
    // once we have submitted all jobs to the thread pool, it should be shutdown
    threadPool.shutdown();
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C# I have an intrusive tree structure that looks like this: public abstract
I have a tree of data that I would like to present to the
I have a tree structure that can be n-levels deep, without restriction. That means
I have category model that has a tree structure. In my database I have
For our CMS we have a site manager that defines the site's tree structure
We have a table with tree structure like this: Id Desc ParentID === A
I have a view that will look like this: I'm trying to figure out
I'm trying to produce a flow diagram of a tree structure. I've been able
I am trying to create a tree structure (a binary tree) that is capable
Let's say that I have an adt representing some kind of tree structure: data

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.