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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:04:46+00:00 2026-05-16T09:04:46+00:00

Is it safe to start a thread pool in an objects constructor? I know

  • 0

Is it safe to start a thread pool in an objects constructor? I know that you shouldn’t start a thread from a constructor, something about the “this” pointer escaping (I don’t exactly understand this, but will do some more searches to try and figure it out).

The code would look something like this:

private ExecutorService pool;

public handler()
{
  pool = Executors.newCachedThreadPool();
}

public void queueInstructionSet(InstructionSet set)
{
  pool.submit(new Runnable that handles this instruction set);
}

If that doesn’t work, i could just create this class as a Runnable and start it in a new thread. However, that seems like it would be adding an unnecessary thread to the program where it doesn’t really need one.

Thanks.

EDIT:

Thanks for the replies everyone, they definitely helped make sense of this.

As per the code, in my mind it makes sense that this constructor creates the thread pool, but let me explain what specifically this code is doing, because i may be thinking about this in a weird way.

The entire point of this object is to take “Instruction Sets” objects, and act on them accordingly. The instruction sets come from clients connected to a server. Once a full instruction set is received from a client, that instruction set is sent to this object (handler) for processing.

This handler object contains a reference to every object that an instruction set can act upon. It will submit the instruction set to a thread pool, which will find which object this instruction set wants to interact with, and then handle the instruction set on that object.

I could handle the instruction set object in the IO server, but my thoughts are having a separate class for it makes the entire code more readable, as each class is focusing on doing only one specific thing.

Thoughts? Advice?

Thanks

  • 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-16T09:04:46+00:00Added an answer on May 16, 2026 at 9:04 am

    Your sample code doesn’t let “this” escape at all. It’s reasonably safe to start a new thread in a constructor (and even use this as the Runnable, which you don’t in this example) so long as you’re sure that you’ve already initialized the object as far as the new thread will need it. For example, setting a final field which the new thread will rely on after starting the thread would be a really bad idea 🙂

    Basically letting the “this” reference escape is generally nasty, but not universally so. There are situations in which it’s safe. Just be careful.

    Having said that, making a constructor start a thread might be seen as doing too much within the constructor. It’s hard to say whether it’s appropriate in this case or not – we don’t know enough about what your code is doing.

    EDIT: Yup, having read the extra information, I think this is okay. You should probably have a method to shut down the thread pool as well.

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

Sidebar

Related Questions

I want to know what is thread safe ? how to start to learn
I know it's safe to draw on any thread so long as I call
I know the API state A thread safe client for working with your SQL
I was reading in MSDN that a List is thread safe when used as
I'm looking to have a class (subclassed from threading.Thread) that is initialised and started
i.e. when I start a new thread, is it safe to read any variables
Possible Duplicate: Java: Why not to start a thread in the constructor? How to
I would like to ask is this code thread safe? There is a problem
In .NET, after this code, what mechanism stops the Thread object from being garbage
I installed LispCabinet and after the first start it tells me: server-ensure-safe-dir: The directory

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.