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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:44:45+00:00 2026-06-11T12:44:45+00:00

I have a task that would benefit from the Thread Pool design pattern (many

  • 0

I have a task that would benefit from the Thread Pool design pattern (many small tasks to be performed in parallel). I initially implemented a naive thread pool from scratch, with n Runnables all pulling work units from the same ConcurrentLinkedQueue until the queue is empty, then terminating. I then decided “hey, let’s try the Executor in Java, because that is probably better-tested and more reliable than my naively designed system.” Problem: in my implementation, each thread persisted until the queue was empty, using a while (!queue.isEmpty()), and got its own instance of a non-threadsafe object, let’s call it SlowObject foo, that is time-consuming to construct. Trying to pass all Runnables that go into the Executor‘s pool an instance of the time-inefficient object fails because it is not thread-safe. Creating a new instance of SlowObject for each Runnable is undesirable because they are costly to construct.

Is there a way to say “how many threads are we using? Let’s create one SlowObject for each thread, and then let the Runnables detect what thread we’re on and look up the correct object to use?” This sounds brittle and failure-prone — not sure what design pattern I should be looking at instead, though.

  • 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-11T12:44:46+00:00Added an answer on June 11, 2026 at 12:44 pm

    Java provides the concept of a ThreadLocal variable.
    You can use it within your Runnable like this.

     public class MyJob implements Runnable {
         private static final ThreadLocal < SlowObject > threadLocal = 
           new ThreadLocal < SlowObject > () {
             @Override protected SlowObject initialValue() {
               // construct and return your SlowObject 
             }
           };
    
         public void run() {
           // work with threadLocal.get()
         }
       }
    

    Thereby for each thread running your Runnable only a single instance of your class SlowObject is created.

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

Sidebar

Related Questions

I have a Rakefile with a Rake task that I would normally call from
I have a process intensive task that I would like to run in the
We have a SharePoint solution that uses a standard task list. We would like
I'm making a task-based program that needs to have plugins. Tasks need to have
I have a task that I perform in parallel; for instance printing user selected
I have a number of macrodef tasks that I would like to call but
I have a data flow task that extracts data from one database and inserts
I have a task that runs periodically 10 second. I do some picturebox refreshing
I have a task that takes a rather long time and should run in
I have a task that's driving me crazy because i have no clue where

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.