Dan Creswell wrote:
On 1 April 2013 08:11, Peter Firmstone <[email protected]> wrote:
Food for thought: After our pending release, it might be an idea to make
a combined effort to identify and address as many concurrency issues as
possible, we need to modernize our implementation code so we stay relevant.
An important task will be updating all our service implementations so they
DON'T start threads during construction.
The ActiveObject pattern often does start threads at construction. I'd like
to understand why that is such a problem for you? It surely isn't a big
deal for me but....
It allows fields to be declared final, if a thread is started during
construction the JMM makes no guarantee that thread will see the final
state of that objects fields after construction completes.
This is important when that thread accesses fields in the constructed
object.
See:
https://www.securecoding.cert.org/confluence/display/java/TSM03-J.+Do+not+publish+partially+initialized+objects
https://www.securecoding.cert.org/confluence/display/java/TSM01-J.+Do+not+let+the+this+reference+escape+during+object+construction
This doesn't mean you can't start a thread during construction, but it
does mean you must be very careful if you do; our old code isn't that
careful. ;)
Cheers,
Peter.