synchronized三種用法 java synchronized鎖對象,當(dāng)對象引用是null的時候,鎖的是什么?
java synchronized鎖對象,當(dāng)對象引用是null的時候,鎖的是什么?謝邀!Java語言規(guī)范中明確指出如果鎖住的對象是null,則會NullPointerException,規(guī)范內(nèi)容如下:
java synchronized鎖對象,當(dāng)對象引用是null的時候,鎖的是什么?
謝邀!
Java語言規(guī)范中明確指出如果鎖住的對象是null,則會NullPointerException,規(guī)范內(nèi)容如下:
The type of Expression must be a reference type, or a compile-time error occurs. A synchronized statement is executed by first evaluating the Expression. Then: If evaluation of the Expression completes abruptly for some reason, then the synchronized statement completes abruptly for the same reason. Otherwise, if the value of the Expression is null, a NullPointerException is thrown.