如何同时修改向量 [英] How to concurrently modify a Vector

查看:204
本文介绍了如何同时修改向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须确保迭代矢量;该向量没有更新,以避免 ConcurrentModificationException 。我可以使用并发收集。但我只是想试一试Vector。下面是我写的代码。

I have to ensure while iterating the Vector; there is no update on that Vector to avoid ConcurrentModificationException. I can use concurrent collection. But i just want to give a try on Vector. Below is the code I have written.

public class TestConcurrentModification1 {
    Vector a = new Vector();

    public static void main(String[] args)  {
        final TestConcurrentModification1 obj = new TestConcurrentModification1();

        new Thread(){
            public  void run(){
                for(int i = 0; i < 5; i++){
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {}
                    obj.a.add(""+i);                
                }
                System.out.println(obj.a);
            }
        }.start();

        new Thread(){
            public  void run(){
                try {
                    Thread.sleep(10);
                    } catch (InterruptedException e) {
                    }
                synchronized (obj.a) {
                    Iterator itr = obj.a.iterator();
                    while(itr.hasNext()) {
                        obj.a.add("TEST");//java.lang.OutOfMemoryError: Java heap space
                        //itr.remove(); //java.lang.IllegalStateException
                    }
                }
            }
        }.start();
    }
}

但上面的代码是抛出1) OutOfMemoryError 或2) IllegalStateException 。请你解释一下是什么导致这两个异常。如何实现我的目标,避免向量

But the above code is throwing 1) OutOfMemoryError OR 2) IllegalStateException. Could you please explain what is causing these two exception. And how to achieve my goal of avoiding ConcurrentModificationException on a Vector?

我必须为Java 1.4.2或之前的版本解决这个问题。

I have to solve this for Java 1.4.2 or prior.

推荐答案

p>

One part of your Problem:

 Iterator itr = obj.a.iterator();
 while(itr.hasNext()) {
     obj.a.add("TEST");// <- if itr.hasNext() would have returned false in next iteration, now it won't
 }

这是一个无限循环每次迭代中的内存使用情况。所以你会迟早遇到一个OutOfMemory。

This is an infinite loop that increases memory usage in each iteration. So you'll run into an OutOfMemory sooner or later.

我建议使用一个好的旧for循环来插入值。如果您真的想要迭代,请使用迭代器)

I suggest using a good old for-loop for inserting values. Use an iterator if you actually want to iterate something :)

更多:您正在与非最终成员进行同步。

More: You are synchronizing against a non-final member.

更多:Iterator.remove throws ...

More: Iterator.remove throws ...


IllegalStateException - 如果下一个方法没有

IllegalStateException - if the next method has not yet been called, or the remove method has already been called after the last call to the next method.

最后但并非最不重要的一点是: Sotirios已经提到的比赛条件(+1)。
每当同步时,请确保您同步关键资源上的每个调用。

And last but not least: The race condition already mentioned by Sotirios (+1 for him). Whenever you synchronize, make sure you synchronize every call on the critical resource.

这篇关于如何同时修改向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆