baidu / Jprotobuf-rpc-socket

Protobuf RPC是一种基于TCP协议的二进制RPC通信协议的Java实现
Apache License 2.0
530 stars 221 forks source link

Double-Checked Locking #66

Open QiAnXinCodeSafe opened 4 years ago

QiAnXinCodeSafe commented 4 years ago

https://github.com/baidu/Jprotobuf-rpc-socket/blob/bab1e2ffc796cd8d31667474b3cdb96bbd24749d/jprotobuf-rpc-core-spring/src/main/java/com/baidu/jprotobuf/pbrpc/spring/annotation/CommonAnnotationBeanPostProcessor.java#L250-L261

Double-Checked Locking is widely cited and used as an efficient method for implementing lazy initialization in a multithreaded environment. Unfortunately, it will not work reliably in a platform independent way when implemented in Java, without additional synchronization. Declare a volatile local variable metadata offers a much more elegant solution