- public class AtomicSingleton
- {
- private static AtomicReference<AtomicSingleton> INSTANCE
- = new AtomicReference<>();
- private AtomicSingleton() {}
- public static AtomicSingleton getInstace(){
- for (;;) {
- AtomicSingleton current = INSTANCE.get();
- if(current != null){
- return current;
- }
- current = new AtomicSingleton();
- if(INSTANCE.compareAndSet(null, current))
- {
- return current;
- }
- }
- }
- }
Atomic单例(乐观锁)
最新推荐文章于 2023-07-31 23:04:15 发布