我有一个类ItemType
,其中包含引用子类实例的final static字段:
public static final ItemType DURATION = new BuiltInAtomicItemType(x);
static class BuiltInAtomicItemType extends ItemType {
public BuiltInAtomicItemType(X x) {
this.x = x;
}
如果一个线程首先加载了超类而另一个线程首先加载了子类,则会引发类加载死锁的可能性。
现在的问题是,这是一个公共API,我不能轻易更改它。我想消除类加载死锁的可能性,但我不想强制更改引用ItemType.DURATION
或其他40多个类似字段的应用程序。
有没有一种方法可以避免潜在的死锁,同时保留公共API?