Java: starting a new thread in a constructor

Starting a thread from the constructor lets the started thread access the object being constructed before it’s properly constructed, and thus makes a not completely constructed object available to the new thread.

You could create the thread in the constructor, and provide a “startup” method to start the thread from the outside.

Or you could make the constructor and startup methods private and provide a static factory method which would create the object, start the thread, and return the created object.

Leave a Comment