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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:15:06+00:00 2026-05-13T23:15:06+00:00

say I’m using a ExecutorService ex = Executors.newFixedThreadPool(nrofthreads); spinning up some work and waiting

  • 0

say I’m using a

ExecutorService ex = Executors.newFixedThreadPool(nrofthreads);

spinning up some work and waiting when it’s done.

However I have same Threadlocal objects in the worker-threads which need to be closed when the batch is done.
Therefore, I would like to be able to call a custom close-method on all worker-threads created by the threadpool.

What would be the most elegant way to do that?

Right now as a hack I’m using:

for(int i =0 ; i<20; i++){ //make sure to touch all threads with 20 runs..
   ex.execute(new Runnable(){
 public void run(){
   tearDownThreadLocals();
 }
   });
}  
ex.shutdown();

but that doesn’t look particulary robust to me 😉

Thanks
GJ

  • 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-13T23:15:07+00:00Added an answer on May 13, 2026 at 11:15 pm

    You can use Executors.newFixedThreadPool(int, ThreadFactory) to pass a ThreadFactory, something like this:

    ExecutorService ex = Executors.newFixedThreadPool(nrofthreads, 
        new ThreadFactory() {
            public Thread newThread(final Runnable r) {
                return new Thread(new Runnable() {
                    public void run() {
                        try {
                            r.run();
                        } finally {
                            tearDownThreadLocals();
                        }
                    }
                });
            }
        });
    

    EDIT: Just noticed that Executors already has a method that accepts a ThreadFactory, so no need to create ThreadPoolExecutor explicitly.

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

Sidebar

Related Questions

Say I have 2 div s with the same CSS class that are stacked
Say I have some pixel buffer associated with variable: CVPixelBufferRef a; I want to
Say you have a button whose command property is bound to some ICommand of
Say we have the following simple data-frame of date-value pairs, where some dates are
Say I have a textbox in HTML using the following code: <input type=text name=text
Say you have a page callback from Module #1 using hook_menu(). You want to
Say I have a select box eg <div data-bind='visible: someProp'> <select class=selectSubWidgets data-bind='options: subWidgets,optionsText:
Say I have a Telerik MVC Grid, AJAX bound and I want to put
Say we have deep hashes like: b = {1 => {2 => {} },
Say i have a few fields like the following: abd738927 jaksm234234 hfk342 ndma0834 jon99322

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.