Skip to content

Concurrency warning #2

Description

@dmlloyd

Note that dynamic constants do not have exclusive initialization like classes do (to draw a parallel to the lazy-holder pattern example). This means that each time a thread encounters an uninitialized dynamic constant, the bootstrap for it is invoked from that thread, which in turn means that the bootstrap may be concurrently invoked. This is especially significant if the initialization is expensive. In this case, the user would need to introduce their own locking strategy.

For more information, see section 5.4.3.6 of the JVM specification, particularly this passage at the bottom of the section:

If several threads attempt resolution of R at the same time, the bootstrap method may be invoked concurrently. Therefore, bootstrap methods which access global application data must take precautions against race conditions.

Also of interest in that section is:

Unlike class initialization (§5.5), where cycles are allowed between uninitialized classes, resolution does not allow cycles in symbolic references to dynamically-computed constants. If an implementation of resolution makes recursive use of a stack, then a StackOverflowError will occur naturally. If not, the implementation is required to detect the cycle rather than, say, looping infinitely or returning a default value for the dynamically-computed constant.

If you put those two things together, it adds up to potential deadlock if user locking is used and there is a cycle in constant initialization.

The documentation should probably be updated to reflect this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions