我在Glassfish 3.1.2.2 上有Java EE应用程序
通过read((,我在下面的代码中得到了一个ConcurrentModificationException:
private Set<MonitoredService> connectedServices = new HashSet<MonitoredService>();
@Override @Lock(LockType.WRITE)
public void addConnectedService(MonitoredService service) {
if (!connectedServices.contains(service)) {
connectedServices.add(service);
}
}
@Override @Lock(LockType.READ)
public Set<MonitoredService> getConnectedServices() {
return Collections.unmodifiableSet(new HashSet<MonitoredService>(connectedServices));
}
我认为Lock注释关心集上的同步访问?
函数同步并不意味着实际集合同步。即使connectedServices
在单例中,您也无法保证其他内容没有修改它。如果您确实需要同步收集,请使用:
Collections.synchronizedSet(...);