Constructors shouldn't start threads
Constructors shouldn't start threads.
According to Java Concurrency in Practice, by Brian Goetz and others (an excellent book), there
are two problems with starting a thread in a constructor (or static initializer):
- in a non-final class, it increases the danger of problems with subclasses
- it opens the door for allowing the
this reference to be passed to another object
before the object is fully constructed
There's nothing wrong with creating a thread object in a constructor or static
initializer - just don't start it there.
The alternative to starting a thread in the constructor is to simply start the thread
in an ordinary method instead.
See Also :