线程和并发修改异常与列表一起使用 [英] Threads and Concurrent Modification Exception working with a list

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

问题描述

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

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 don't comment on the arraylist / list but rather the error itself.

有一个并发修改异常经常发生,但并非总是尝试执行 output.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 outputting it or adding a word to it.

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

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

在这种情况下,因为 output.write(mylist )可能正在列表中进行迭代,您不能使用 Collections.synchronizedList(...)方法,因为迭代器需要通过

In this case, because output.write(mylist) is probably iterating through the list, you cannot use the Collections.synchronizedList(...) method because the iterators need to be synchronized by the caller.

如果这种高性能方法被称为无数次,那么您也可以使用 ConcurrentLinkedQueue 但这显然是队列,而不是列表。 ConcurrentSkipList 是另一个选择,尽管它是一个较重的数据结构。

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. ConcurrentSkipList is another option although that is a heavier data structure.

这篇关于线程和并发修改异常与列表一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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