我有一个 WeakReference 的 Collections.synchronizedList,_components;
我写了如下内容,希望编译器抱怨:
public boolean addComponent2(Component e) {
synchronized (_components) {
return _components.add(new WeakReference<Component>(e));
}
}
但是编译器完全满意。请注意,List.add() 返回 TRUE。好吧,从同步块的任何退出都会释放锁,但这看起来不奇怪吗?它有点像块中的“洞”,类似于在循环中使用 return。
你愿意维护这样的代码吗?