美文网首页
JDK8 HashMap死锁demo

JDK8 HashMap死锁demo

作者: 牧码人zhouz | 来源:发表于2021-02-27 14:47 被阅读0次

    示例代码如下:

    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里程序也一直卡住,无法结束:


    相关文章

      网友评论

          本文标题:JDK8 HashMap死锁demo

          本文链接:https://www.haomeiwen.com/subject/skfifltx.html