Collections.sort 的多线程安全问题

背景

在实际的工作中,遇到了多线程操作 Collections.sort 导致了 ConcurrentModificationException。多线程操作了同一个共享变量导致。

解决办法可以加锁。

package com.test.wyf.springprofile;

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

public class MultiThreadCollectionsSort {
    public static void main(String[] args) {
        System.out.println("main start......");

        List<String> list = new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");

        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                Collections.sort(list);
                System.out.println("thread done...");
            }).start();
        }


        System.out.println("main done......");
    }
}

有时报错

main start......
main done......
thread done...
Exception in thread "Thread-1" Exception in thread "Thread-2" java.util.ConcurrentModificationException
	at java.util.ArrayList.sort(ArrayList.java:1466)
	at java.util.Collections.sort(Collections.java:143)
	at com.test.wyf.springprofile.MultiThreadCollectionsSort.lambda$main$0(MultiThreadCollectionsSort.java:18)
	at java.lang.Thread.run(Thread.java:748)
java.util.ConcurrentModificationException
	at java.util.ArrayList.sort(ArrayList.java:1466)
	at java.util.Collections.sort(Collections.java:143)
	at com.test.wyf.springprofile.MultiThreadCollectionsSort.lambda$main$0(MultiThreadCollectionsSort.java:18)
	at java.lang.Thread.run(Thread.java:748)

有时候不报错(较少时候,试了很多次才出现如下)

main start......
main done......
thread done...
thread done...
thread done...

代码改进,可以将list对象作为锁对象

public static void main(String[] args) {
        System.out.println("main start......");

        List<String> list = new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");

        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                synchronized (list) {
                    Collections.sort(list);
                }
                System.out.println("thread done...");
            }).start();
        }


        System.out.println("main done......");
    }

你可能感兴趣的:(Java高级,java,jvm,servlet)