...
Wiki Markup |
---|
Statically initialized fields are guaranteed to be fully constructed before they are made visible to other threads (see [CON26CON28-J. Do not publish partially initialized objects] for more information). Consequently, the background thread must wait for the main (or foreground) thread to finish initialization before it can proceed. However, the {{ConnectionFactory}} class's main thread invokes the {{join()}} method which waits for the background thread to finish. This interdependency causes a class initialization cycle that results in a deadlock situation \[[Bloch 05b|AA. Java References#Bloch 05b]\]. |
...
Wiki Markup |
---|
<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:macro-id="ef0eea83dc956949-246b397f-4ce24239-af16b1b8-f652bc57ee91b9fb3d732850"><ac:parameter ac:name="">CON20-EX1</ac:parameter></ac:structured-macro> *CON20-EX1:* It is permissible to start a background thread during class initialization provided the thread does not access any fields. For example, the {{ObjectPreserver}} class (based on \[[Patterns 02|AA. Java References#Patterns 02]\]) shown below provides a mechanism for storing object references, which prevents an object from being garbage-collected, even if the object is not dereferenced in the future. |
...