Java线程和并发修改例外 [英] Java Threads and Concurrent Modification Exception

查看:168
本文介绍了Java线程和并发修改例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个愚蠢的问题,但无法解决如何解决这个问题,我以前没有太多使用线程的经验。

I know this is a daft questions but cant work out how to fix this, I have not had much experience with using threads before.

下面应该创建第一个所有一个定时器,它将每10秒执行一次命令output.write(mylist),这将只输出mylist的内容。

Below should create first of all a timer which will execute the command output.write(mylist) every 10 seconds which will simply output the contents of mylist.

其次它循环通过我有的3个列表并为每个人创建一个线程,它将继续循环获取列表中的下一个单词。
请注意:这是精简版并没有完整所以请不要评论arraylist / list而不是错误本身。

Secondly it loops through about 3 lists I have and for each them creates a thread which will continue to loop through getting the next word in the list. Please note: this is stripped down version and not complete so please dont comment on the arraylist / list but rather the error itself.

有一个并发修改异常频繁发生,但不是所有的时候它都尝试输出.write

我猜这是因为其他一个线程当前正在保存一些东西到mylist?我该怎么办呢?

There is a concurrent modification exception happening frequently but not all the time when it tries to do output.write
I am guessing this is because one of the other threads are currently saving something to mylist? How would I go about fixing this?

非常感谢!

    Runnable timer = new Runnable() {
        public void run() {
            try {
                while (true) {
                    Thread.sleep(10000);
                    output.write(mylist);
                }
            } catch (InterruptedException iex) {}
        }
    };
    Thread timerThread = new Thread(timer);
    timerThread.start();

    for (final ValueList item : list) {

        Runnable r = new Runnable() {
            public void run() {
                try {
                    while (true) {

                        char curr = item.getNext();

                         mylist.addWord(curr);
                    }
                } catch (InterruptedException iex) {}
            }
        };

        Thread thr = new Thread(r);
        thr.start();
    }
}


推荐答案


有一个并发的修改异常经常发生,但并不是所有的时候它都试图 output.write ...

问题是(我假设) output.write(...)方法正在迭代你的 myList 同时另一个线程正在调用 myList.addWord(curr); 。除非 myList 是并发集合,否则不允许这样做。

The problem is (I assume) that the output.write(...) method is iterating through your myList at the same time as another thread is calling myList.addWord(curr);. This is not allowed unless myList is a concurrent collection.


我怎么去关于解决此问题?

How would I go about fixing this?

每次你需要在 myList 上进行同步正在访问它 - 在这种情况下,当你输出它或添加一个单词时。

You will need to synchronize on myList every time you are accessing it -- in this case when you are outputing it or adding a word to it.

  synchronized (myList) {
      output.write(mylist);
  }
  ...

  synchronized (myList) {
      myList.addWord(curr);
 }

您还可以定义 myList 作为同步列表,在这种情况下, Collections 包装器类将为您处理同步。

You could also define myList as a synchronized list in which case the Collections wrapper class will take care of the synchronization for you.

  List<String> myList = Collections.synchronizedList(new ArrayList<String>());

如果这是一种被称为吨次的高性能方法,那么你也可以使用 ConcurrentLinkedQueue 但这显然是一个不是列表的队列。

If this is some high-performance method that is called tons of times then you could also use a ConcurrentLinkedQueue but that is a queue not a list obviously.

这篇关于Java线程和并发修改例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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