ConcurrentModificationException only in Java 1.8.0_45

前端 未结 4 925
余生分开走
余生分开走 2021-01-19 10:36

I\'ve got two question about this code:

import java.util.*;

public class TestClass {

    private static List list;   
    public static void          


        
4条回答
  •  天命终不由人
    2021-01-19 11:01

    You're getting this exception because you have separate threads modifying and iterating the list at the same time.

    The commented out sort is not causing the problem. The CME is caused by the sort and the iteration inside the thread. Since you have multiple threads sorting and iterating, you're getting a CME. This is not dependent on the Java version.

    It looks like your threads don't need to modify the list, so you can perform the sort once before your loop that creates threads, then remove if from the thread.

提交回复
热议问题