JDK8 HashMap死锁demo

2021-02-27  本文已影响0人  牧码人zhouz

示例代码如下:

public class TestHashMap {
    static HashMap<UUID, UUID> hashMap = new HashMap<>();
    static final int THREAD_COUNT = 100;
    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[THREAD_COUNT];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    hashMap.put(UUID.randomUUID(), UUID.randomUUID());
                }
                System.out.println(Thread.currentThread().getName() + " Finish!! ");
            }, "T" + i);
        }

        for (Thread thread : threads) {
            thread.start();
        }
        for (Thread thread : threads) {
            thread.join();
        }
    }
}

控制台截图:



可以看到,程序已经运行的八分钟多,还没有运行完,而CPU一直保持在100%左右。

Idea里程序也一直卡住,无法结束:


上一篇下一篇

猜你喜欢

热点阅读