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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:00:39+00:00 2026-06-13T15:00:39+00:00

I have multiple runnable classes and I want to run them with executor service

  • 0

I have multiple runnable classes and I want to run them with executor service through a centralized class (call it Launcher class which contains list of all runnables).

I wrote a Launcher class which takes all the beans to instantiate using applicationContext.getBean(). Each runnable class also defines a pool size for it (number of threads to spawn for this runnable).

public class DaemonsLauncher {

    @Autowired
    private ApplicationContext appContext;

    private List<Daemon> daemons = new ArrayList<Daemon>();

    private ScheduledThreadPoolExecutor executor;

    private void initThreadPool() throws Exception {
        //daemonNames coming at run time from somewhere.
        List<String> daemonList = Arrays.asList(daemonNames.split(Constant.COMMA));
        //Pool size will now be summation of all the pool sizes.
        int poolSize = 0;
        for (String dName : daemonList) {
            Daemon daemon = appContext.getBean(dName, Daemon.class);
            poolSize += daemon.getPoolSize();
            daemons.add(daemon);
        }
        executor = new ScheduledThreadPoolExecutor(poolSize);
    }

    public void launchDaemons() throws Exception {
        for (Daemon daemon : daemons) {
            for (int currentThreadCount = 1; currentThreadCount <= daemon.getPoolSize(); currentThreadCount++) {
                executor.scheduleWithFixedDelay(daemon, 0, XYZ, ABC);
            }
        }
    }
}

In the process of doing it, I add all runnables to a List<Daemon> (where Daemon is a abstract runnable class which is extended by other daemons).

public abstract class Daemon implements Runnable {

    protected int poolSize = 1;

    public int getPoolSize() {
        return poolSize;
    }

    @Override
    public void run() {
        //Calls run methods of individual runnables here.
    }

}

As you can see, I add same instance of each runnable class multiple times(depending on poolSize) while executing.

executor being a ScheduledThreadPoolExecutor.

The moment, run method of R is called, I find same instance of runnable class both the times since they were taken only once by appContext.getBean() (checked by printing hasCode and toString).

But these multiple instances of runnable class run at the same time.

What might be the issue with this sort of case? Are these multiple threads running for same runnable will cause issues or it will go fine?

As an alternative, depending on poolSize, I can get multiple instances of the Runnable class even before this loop and loop only once depending on all the ‘Runnable’ entries present in the List.

  1. All beans are prototype in nature (though it will not come into picture as I have called appContext.getBean only once).
  2. A sample Runnable class.

    public class R extends Daemon {
        @Override
        public void runIndividualDaemon() {
            //Called by run of Daemon class.
            logger.info("Running Daemon." + this.hashCode() + " " + this.toString());
        }
    
        @Override
        protected void init() throws Exception {
            this.poolSize = 5;
        }
    }
    
  • 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-13T15:00:40+00:00Added an answer on June 13, 2026 at 3:00 pm

    If you have a task being performed multiple times, it is because you added it multiple times.

    I suggest you log when you add tasks to confirm this.

    But how can it happen that same thread is performing task of two threads?

    A thread cannot perform two tasks at once, but it can perform one task after another, re-using the same thread.

    Is it that “I have two instances of ‘Runnable’ class” which would not have been the case if that bean was singleton?

    I don’t believe add two tasks which are the same or adding the same task twice will make much difference in this case.

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

Sidebar

Related Questions

I have a class,which implements Runnable interface. I want to create multiple threads for
I have a runnable jar, which contains multiple classes within a package. I am
I have multiple domains on my server. I want to redirect all of them
I have multiple arrays with string values and I want to compare them and
Let's say you have N runnable objects, and for each you want them to
I have a single maven project that has multiple main classes. I want to
I have multiple projects which are to be hosted together in a Tomcat container,
I have multiple forms for lots of products on my page and want to
I have multiple input fields on my page. I Regex them with a css
I have multiple input text fields grouped in divs: <div class=container0> <input class=containerItem0 name=containerItem[0][0]

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.