Java集已满

Java集已满,java,set,concurrentmodification,Java,Set,Concurrentmodification,我正在制作一个粒子发射器。 每个“渲染”对象都存储在哈希集中,当屏幕上有很多粒子时,控制台会抛出并发修改异常。我通常对这些粒子的寿命很短,所以它们会在几秒钟后被删除,但我相信这在将来可能会成为一个问题。我怎样才能解决这个问题 编辑:代码: public class UpdatedManager { private static Set<Updated> updates = new HashSet<>(); private UpdatedManager() {} pub

我正在制作一个粒子发射器。 每个“渲染”对象都存储在哈希集中,当屏幕上有很多粒子时,控制台会抛出并发修改异常。我通常对这些粒子的寿命很短,所以它们会在几秒钟后被删除,但我相信这在将来可能会成为一个问题。我怎样才能解决这个问题

编辑:代码:

public class UpdatedManager {
private static Set<Updated> updates = new HashSet<>();

private UpdatedManager() {}

public static Set<Updated> getUpdates() {
    return new HashSet<Updated>(updates);
}
public static boolean registerUpdated(Updated u) {
    return updates.add(u);
}
public static boolean unregisterUpdated(Updated u) {
    return updates.remove(u);
}
public static void update() {
    for (Updated up : new HashSet<Updated>(updates))
        up.update();
}
public static Set<GameObject> getGameObjects() {
    Set<GameObject> objs = new HashSet<>();
    for (Updated up : new HashSet<Updated>(updates)) {
        if (up instanceof GameObject)
            objs.add((GameObject) up);
    }
    return objs;
}
public static Set<GameObject> getGameObjectsByName(String name) {
    Set<GameObject> objs = new HashSet<>();
    for (GameObject go : new HashSet<GameObject>(getGameObjects())) {
        if (go.getName() != null && go.getName().equals(name))
            objs.add(go);
    }
    return objs;
}
public static Set<Particle> getParticles() {
    Set<Particle> parts = new HashSet<>();
    for (Updated up : new HashSet<Updated>(updates)) {
        if (up instanceof Particle)
            parts.add((Particle) up);
    }
    return parts;
}
}
公共类更新管理器{
私有静态集更新=新HashSet();
私有更新管理器(){}
公共静态集getUpdates(){
返回新的哈希集(更新);
}
公共静态布尔寄存器更新(u更新){
返回更新。添加(u);
}
公共静态布尔值未注册更新(更新u){
返回更新。删除(u);
}
公共静态无效更新(){
for(更新:新哈希集(更新))
up.update();
}
公共静态集getGameObjects(){
Set objs=new HashSet();
for(更新:新哈希集(更新)){
if(游戏对象的上一个实例)
添加((游戏对象)up);
}
返回对象;
}
公共静态集getGameObjectsByName(字符串名称){
Set objs=new HashSet();
for(GameObject go:newhashset(getGameObjects())){
如果(go.getName()!=null&&go.getName().equals(name))
objs.add(go);
}
返回对象;
}
公共静态集getParticles(){
Set parts=new HashSet();
for(更新:新哈希集(更新)){
if(粒子的上实例)
部分。添加((粒子)向上);
}
返回部件;
}
}

是什么让你认为这一套已经满了

并发修改异常意味着不同线程正在以不安全的方式访问集合

使用Collections实用程序尝试同步设置

HashSet hashSet = new HashSet();
Set set = Collections.synchronizedSet(hashSet);

或者对访问集合的方法使用synchronized关键字。

ConcurrentModificationException表示您在迭代集合时修改了集合。这并不意味着布景已经满了


例如,以下代码将抛出ConcurrentModificationException:

Set<String> set = new HashSet<>();

set.add("Hello");

for(String s : set)
    set.add(s+" world");
Set Set=newhashset();
set.add(“Hello”);
for(字符串s:set)
集合。添加(s+“世界”);

请注意,不能保证获得ConcurrentModificationException,因此应避免捕获它。相反,您应该修复代码,以免引起问题。

ConcurrentModificationException通常发生在您对集合进行更改(如删除元素)时,同时对集合进行迭代。您需要重新设计解决该问题的方法。引发异常的行是哪一行?我循环遍历集合的副本,而不是实际集合。@MCMastery如果您循环遍历集合的副本(如
for(String s:new HashSet(set))
中所述),则不会得到ConcurrentModificationException(因为您没有修改副本)。您得到一个ConcurrentModificationException,因此您没有在集合的副本中循环。如果它说发生了错误,那么它就是一个副本。我将再次检查它,因为它建议synchronizedSet将在不知道根本原因的情况下解决所有OP的问题。synchronizedSet不会解决所有ConcurrentModificationExceptioneption问题。即使是由多线程引起的问题。