本文介绍了Collections.sort方法有时会在多线程环境中引发ConcurrentModificationException.列表未在结构上进行修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    package CollectionsTS;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.List;

    public class ArrayListTS {
        public static void main(String[] args) {
            HashSet<Integer> hset = new HashSet<Integer>();
            for (int i = 0; i <= 1000; i++) {
                hset.add(i);
            }

            MyRunnable mr = new MyRunnable();
            mr.addElements(hset);

            Thread t1 = new Thread(mr,"t1");
            Thread t2 = new Thread(mr,"t2");
            Thread t3 = new Thread(mr,"t3");

            t1.start(); t2.start(); t3.start();

        }
    }

    class MyRunnable implements Runnable {

        List<Integer> ilist = new ArrayList<Integer>();

        public void addElements(HashSet<Integer> hset) {
            ilist.addAll(hset);
        }

        @Override
        public void run() {
            Collections.sort(ilist);
            if (ilist.size() > 0)
                System.out.println( Thread.currentThread().getName() +" = "+ilist.get(ilist.size() - 1));
            else
                System.out.println("List is empty");
        }
    }

抛出的异常是ConcurrentModificationException,我想知道代码没有修改列表(不是结构上的).

Excption thrown is ConcurrentModificationException , I am wondering the code is not modifying the list (not structurally) .

Exception in thread "t1" t3 = 1000
Exception in thread "t2" java.util.ConcurrentModificationException
    at java.util.ArrayList.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at CollectionsTS.MyRunnable.run(ArrayListTS.java:37)
    at java.lang.Thread.run(Unknown Source)
java.util.ConcurrentModificationException
    at java.util.ArrayList.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at CollectionsTS.MyRunnable.run(ArrayListTS.java:37)
    at java.lang.Thread.run(Unknown Source)

我有一个方法可以返回列表中的最大值,我不想使用usr Collections.max(),我想借助collections.sort方法在多线程环境中对列表进行排序.

I have method which returns the maximum out of list , I don't want to usr Collections.max() , I want to sort the list in multithreaded env with help of collections.sort method.

Collections.sort方法有时会在多线程环境中引发ConcurrentModificationException.列表未在结构上进行修改.

Collections.sort method sometimes throws ConcurrentModificationException in multithreaded environment . List is not being modified structurally.

有人可以帮我吗?

推荐答案

您已经创建了一个MyRunnable实例,该实例具有ArrayList作为成员变量.然后在3个单独的线程中尝试对ArrayList进行排序.调用sort将在结构上修改列表.这就是为什么它导致ConcurrentModificationException.

You have created a single MyRunnable instance, which has an ArrayList as a member variable. Then in 3 separate threads you attempt to sort the ArrayList. Calling sort will structurally modify the list. That is why it results in a ConcurrentModificationException.

这篇关于Collections.sort方法有时会在多线程环境中引发ConcurrentModificationException.列表未在结构上进行修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:11