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 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

Ask A Question

Stats

  • Questions 502k
  • Answers 502k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The comma operator evaluates both of its operands (from left… May 16, 2026 at 2:42 pm
  • Editorial Team
    Editorial Team added an answer With a _show function, there are three parts involved: The… May 16, 2026 at 2:42 pm
  • Editorial Team
    Editorial Team added an answer It is located in c:\program files\microsoft sql server\xxx\sdk\assemblies on my… May 16, 2026 at 2:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm seeing this code in a project and I wonder if it is safe
In my current C#/NET 3.5 application, I have a task queue (thread safe) and
So this is a continuation from my last question - So the question was
When using Aptana with PHP 5.2.3 in debug mode (using thread safe XDebug 2.1.0
I'm writing thread-safe class in C++. All of its public methods use locks (non-recursive
I have a class that implements Runnable and am currently using an Executor as
I have built software that I deploy on Windows 2003 server. The software runs
I am using a System.Threading.ThreadPool to manage a queue of jobs from a service.
I find myself wanting to write a routine that will operate on both volatile
Ruby's safe mode disallows the use of tainted data by potentially dangerous operations. It

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.