synchronized三种用法 java synchronized锁对象,当对象引用是null的时候,锁的是什么?

java synchronized锁对象,当对象引用是null的时候,锁的是什么?谢邀!Java语言规范中明确指出如果锁住的对象是null,则会NullPointerException,规范内容如下:

java synchronized锁对象,当对象引用是null的时候,锁的是什么?

谢邀!

Java语言规范中明确指出如果锁住的对象是null,则会NullPointerException,规范内容如下:

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.