鍍金池/ 問答/Java  Linux/ Java: 多線程同步中偶爾多個線程同入synchronized塊?

Java: 多線程同步中偶爾多個線程同入synchronized塊?

源碼如下:

public class Main {
    public static void main(String[] args) {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
        HashSet<String> s = new HashSet<>();
        String id = "x12323";
        for(int j = 0; j<20; j++) {
            fixedThreadPool.execute(() -> {
                System.out.println("運行線程: " + Thread.currentThread().getName());
                String lockObj = s.getClass().toString() + id; 
                synchronized (lockObj) {
                    if (s.isEmpty()) {
                        System.out.println(Thread.currentThread().getName() + " : lockObj(" + lockObj+ ") hash code ("
                            + lockObj.hashCode() + "),集合 s 為空嗎:" + s.isEmpty());
                    s.add("good");
                    }
                }
            });
        }
    }
}

運行結(jié)果:


    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-2
    pool-1-thread-1 : lockObj(class java.util.HashSetx12323) hash code (-1786802297),集合 s 為空嗎 :true
    pool-1-thread-2 : lockObj(class java.util.HashSetx12323) hash code (-1786802297),集合 s 為空嗎:true
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-1
    運行線程: pool-1-thread-5
    運行線程: pool-1-thread-4
    運行線程: pool-1-thread-3

為何這兩個線程都進入互斥塊了?

    pool-1-thread-1 : lockObj(class java.util.HashSetx12323) hash code (-1786802297),集合 s 為空嗎 :true
    pool-1-thread-2 : lockObj(class java.util.HashSetx12323) hash code (-1786802297),集合 s 為空嗎:true
回答
編輯回答
忠妾

更新 編譯后常量池和字節(jié)碼

默認編譯 Java 8 查看編譯后的 class 文件

constant_pool_count 為 146,
其中只有

#005 (String): x12323
#013 (String): 運行線程:
#020 (String): :lockObj(
#021 (String): hash code (
#024 (String): ),集合 s 為空嗎:
#026 (String): good
#030 - #052 (UTF-8)
#056 - #058 (UTF-8)
#060 - #061 (UTF-8)
#065 - #066 (UTF-8)
#070 (UTF-8): BootstrapMethods
#078 - #079 (UTF-8)
#087 - #088 (UTF-8)
#091 (String): ),集合 s 為空嗎:
#093 (String): good
#095 - #107 (UTF-8)
#110 - #133 (UTF-8)
#137 - #138 (UTF-8)
#140 - #142 (UTF-8)
#144 - #0145 (UTF-8)

再看字節(jié)碼部分:

clipboard.png

037: aload_0 是從局部變量表里加載0號值
041: ldc #5->x12323 是加載常量池6號值

所以說 classname 不是常量值入棧,個人猜測是因為

有泛型之后,Java 又選擇了泛型擦除,很多東西要等到運行時才能確定,getClass 這個操作應該就是帶有不確定性,所以它的結(jié)果不進入常量池。

更新 顯示更多的調(diào)試信息

右擊變量區(qū),點擊 Customize Data Views...

clipboard.png

選你要的,我是全部勾選了。

clipboard.png

原答案

hasCode 一樣并不說明兩個 String 實例是同一個,String.hashCode只是計算value值,同樣內(nèi)容的情況下 hashCode 會一樣的。

String lockObj = s.getClass().toString() + id; 后邊加一句打印語句并下斷點。

clipboard.png

第一次循環(huán)時創(chuàng)建的 lockObj 的內(nèi)存數(shù)據(jù)是這樣的

clipboard.png

這是第二次的內(nèi)存數(shù)據(jù)。

可以看到,這里的兩個 String 的值即使一樣,它們的 value 屬性卻不是同一個指向。

所以你鎖的對象其實本就不是同一個,把 lockObj 的聲明放到循環(huán)外部后你鎖的才是同一個對象。

個人猜測:

你的 lockObj 是一個依賴運行時變量hashCode而拼接的字符串,而且不是常量字符串的拼接,也就是說編譯器無法在編譯期優(yōu)化掉它,也就不會在字符串池里放這個。

也就是說,每次循環(huán)時 JVM 會重新創(chuàng)建一個 byte[],這就導致了每次 lockObj 實際上不是同一個實例。

2018年8月31日 15:29