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

喜欢而已 提交于 2019-12-23 17:16:25

问题


    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");
        }
    }

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)

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 method sometimes throws ConcurrentModificationException in multithreaded environment . List is not being modified structurally.

Could anyone help me on this ?


回答1:


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.



来源:https://stackoverflow.com/questions/29359791/collections-sort-method-sometimes-throws-concurrentmodificationexception-in-mult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!