使用枚举类型创建的单例,线程安全问题

Singleton created with enum type, problems with threads safety

美好的一天,我已经创建了单例:

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;

public enum Singleton {
    FIRST_INSTANCE;

    String[] scrabbleLetters = {
            "a","a","a","a","a","a","a","a","a","b","b","b","b","b","b","b","b","b",
            "c","c","c","c","c","c","c","c","c","d","d","d","d","d","d","d","d","d","d",
    };

    private LinkedList<String> letterList = new LinkedList<>(Arrays.asList(scrabbleLetters));

    private Object lock = new Object();

    private Singleton() {
        Collections.shuffle(letterList);
    }

    public static Singleton getInstance() {
        return FIRST_INSTANCE;
    }

    public LinkedList<String> getLetterList() {
        synchronized (lock) {

        return FIRST_INSTANCE.letterList;
        }
    }

    public LinkedList<String> getTiles(int howManyTiles) {
        synchronized (lock) {

        LinkedList<String> tilesToSend = new LinkedList<>();
        for(int i=0; i<= howManyTiles; i++) {
            tilesToSend.add(FIRST_INSTANCE.letterList.remove(0));
        }
        return tilesToSend;

        }
    }

}

我已经用这个例子测试了它的线程安全性:

import java.util.LinkedList;

public class ScrabbleTest {
    public static void main(String[] args) {
        Runnable getTiles = () -> {

            System.out.println("In thread : " +Thread.currentThread().getName());
            Singleton newInstance = Singleton.getInstance();
            System.out.println("Instance ID: " + System.identityHashCode(newInstance));
            System.out.println(newInstance.getLetterList());

            LinkedList<String> playerOneTiles = newInstance.getTiles(7);
            System.out.println("Player : " + Thread.currentThread().getName() + playerOneTiles);
            System.out.println("Got Tiles for " + Thread.currentThread().getName());
        };

        new Thread(getTiles, "First").start();
        new Thread(getTiles, "Second").start();
    }
}

执行了 10 次后,我确定没有问题,但是当我 运行 最后一次收到这个堆栈跟踪时:

In thread : Second
In thread : First
Instance ID: 1380197535
Instance ID: 1380197535
[d, d, b, c, b, b, a, d, c, d, a, d, c, a, a, d, c, a, a, b, d, b, b, a, b, c, a, d, c, a, c, b, c, c, b, d, d]
Player : First[d, d, b, c, b, b, a, d]
Got Tiles for First
Exception in thread "Second" java.util.ConcurrentModificationException
    at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
    at java.util.LinkedList$ListItr.next(Unknown Source)
    at java.util.AbstractCollection.toString(Unknown Source)
    at java.lang.String.valueOf(Unknown Source)
    at java.io.PrintStream.println(Unknown Source)
    at ScrabbleTest.lambda[=12=](ScrabbleTest.java:10)
    at java.lang.Thread.run(Unknown Source)

这种异常很少发生,大约20次执行1次。 我发现 ConcurrentModificationException 可能会被检测到对象的并发修改的方法抛出,而这种修改是不允许的。在代码中,我有一个锁可以防止这种情况发生,有一个相同的锁用于更改和检索同步块的列表。我什至无法想象为什么会这样。

CME 与并发性的关系并不像它的名字让您想到的那么多。 CME 最常见的情况是在单线程上下文中。然而,在这种情况下,也涉及线程。

您的问题来自 tilesToSend.add(FIRST_INSTANCE.letterList.remove(0));,您在其中修改了 letterList,但它同时被 println 迭代。同步在这里无济于事,因为您必须同步比实际可能的块大得多的块。

这里的简单解决方案是 return getLetterList() 中列表的副本,例如

return new LinkedList<>(FIRST_INSTANCE.letterList);

这样可以在 println 迭代副本的同时 remove() 修改原始列表。