...
Wiki Markup |
---|
Statically initialized fields are guaranteed to be fully constructed before they are made visible to other threads (see [CON28-J. Do not publish partially initialized objects|CON28TSM03-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="cbedd32fdd02145c-7e83ee50-4f474924-9b2ba9f0-42e7a3fcb62b03028e30c313"><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. |
...