Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Static initializers referring to subclasses: avoid class loading deadlock

I have a class ItemType with final static fields that refer to instances of a subclass:

public static final ItemType DURATION = new BuiltInAtomicItemType(x);

static class BuiltInAtomicItemType extends ItemType {

    public BuiltInAtomicItemType(X x) {
        this.x = x;
    }

And this gives rise to the possibility of a class loading deadlock if one thread loads the superclass first while the other class loads the subclass first.

Now the problem is that this is a public API and I can't easily change it. I want to remove the possibility of a class loading deadlock, but I don't want to force change on applications that refer to ItemType.DURATION or the other 40-odd similar fields.

Is there a way to avoid the potential deadlock while retaining the public API?

like image 556
Michael Kay Avatar asked Mar 17 '26 00:03

Michael Kay


1 Answers

Status report: no-one seems to know a solution to this problem. Designing a class structure in a complex application to avoid all possibility of deadlocks during class loading and initialization appears to be very difficult. Modifying an existing application to eliminate such possibilities without making incompatible changes to public APIs appears in the general case to be impossible.

like image 185
Michael Kay Avatar answered Mar 19 '26 14:03

Michael Kay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!