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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:53:02+00:00 2026-06-10T00:53:02+00:00

I have a problem that seems close to what Executor s and Thread pools

  • 0

I have a problem that seems close to what Executors and Thread pools do, however I can’t seem to make it exactly fit. Basically I have workers which take a bit of time to initialize and that I’d like to pool, and once they are ready I use them to do work. I need to do this in a Thread :

worker = buildExpensiveWorker();
worker.doWork(work1);
worker.doWork(work2);
worker.doWork(work3);
...

While an Executor only allows me to do this :

doWork(work1);
doWork(work2);
doWork(work3);
...

Will I need to write my own Thread pool ? It feels like a shame to rewrite what is already well done. Or will I need to use ThreadLocal to hold my workers, and manage them from inside the Runnable‘s run() method ?

  • 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-10T00:53:03+00:00Added an answer on June 10, 2026 at 12:53 am

    If you’re talking about actually initializing the Thread objects prior to them being available for use, take a look at ThreadPoolExecutor.setThreadFactory:

    http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html#setThreadFactory(java.util.concurrent.ThreadFactory)

    You can provide your own implementation create a thread in any manner you want, including creating custom Thread subclasses and/or custom initialization.

    I would say however, that if your initialization is expensive, you should probably try to configure the executor to keep the actual threads alive as long as possible (ie, set the keep alive time rather high, and try to spin up all the threads you need right away, and take the hit up front).

    EDIT: Using a thread local (as mentioned in the comments):

    public class ThreadData {
        public static final ThreadLocal<String> data = new ThreadLocal<String>();
    }
    
    public class InitializingThread extends Thread {
        public InitializingThread(Runnable r) {
            super(r);
        }
    
        public void run() {
            ThreadData.data.set("foo");
            super.run();
        }
    }
    
    public class InitializingThreadFactory implements ThreadFactory {
        public Thread newThread(Runnable r) {
            return new InitializingThread(r);
        }
    }
    
    ThreadPoolExecutor executor = ...;
    executor.setThreadFactory(new InitializingThreadFactory());
    executor.execute(...);
    

    And then in your Runnable:

    public void run() {
         String s = ThreadData.data.get();
    }
    

    Also, this approach (as opposed to using using Thread.currentThread() and casting) has the advantage of being able to actually be used with any Thread implementation (including the default), or without a thread (directly calling the .run() method after setting the value in the ThreadLocal). You could also easily change “ThreadLocal” to “InheritableThreadLocal”, and set it once before submitting anything to the thread pool. All child threads would inherit the value from their parent thread (the one which created the pool).

    It’s important to note that the “run” method of a given thread will only ever executed once, even when in a thread pool, so this guarantees that your initialization routine happens on a per thread basis.

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

Sidebar

Related Questions

I have a strange problem that I can't seem to solve. I've quite a
I have a problem that seems so senseless that I'm sure I'm missing something
I have a problem that I have not faced before: It seems that the
I'm facing a problem that seems to have no straighforward solution. I'm using java.util.Map
I have a problem with something that otherwise seems to be an easy task.
Seems to be a problem that many people have, but all the answers I
I have an issue that seems like very flaky behavour, is this a problem
Had a problem with the recursive conflictCheck() method. That seems fine now. I have
This seems like a simple problem: I have a WF4 activity that guides the
I have a very simple C# DataTable problem that I cannot seem to wrap

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.