double-checked-locking

double check locking without volatile (but with VarHandle release/acquire)

橙三吉。 提交于 2021-02-13 12:14:31
问题 The question is rather easy, in a way. Suppose I have this class: static class Singleton { } And I want to provide a singleton factory for it. I can do the (probably) obvious. I am not going to mention the enum possibility or any other, as they are of no interest to me. static final class SingletonFactory { private static volatile Singleton singleton; public static Singleton getSingleton() { if (singleton == null) { // volatile read synchronized (SingletonFactory.class) { if (singleton ==

double check locking without volatile (but with VarHandle release/acquire)

為{幸葍}努か 提交于 2021-02-13 12:13:37
问题 The question is rather easy, in a way. Suppose I have this class: static class Singleton { } And I want to provide a singleton factory for it. I can do the (probably) obvious. I am not going to mention the enum possibility or any other, as they are of no interest to me. static final class SingletonFactory { private static volatile Singleton singleton; public static Singleton getSingleton() { if (singleton == null) { // volatile read synchronized (SingletonFactory.class) { if (singleton ==

double check locking without volatile (but with VarHandle release/acquire)

别等时光非礼了梦想. 提交于 2021-02-13 12:12:39
问题 The question is rather easy, in a way. Suppose I have this class: static class Singleton { } And I want to provide a singleton factory for it. I can do the (probably) obvious. I am not going to mention the enum possibility or any other, as they are of no interest to me. static final class SingletonFactory { private static volatile Singleton singleton; public static Singleton getSingleton() { if (singleton == null) { // volatile read synchronized (SingletonFactory.class) { if (singleton ==

double check locking without volatile (but with VarHandle release/acquire)

烂漫一生 提交于 2021-02-13 12:12:31
问题 The question is rather easy, in a way. Suppose I have this class: static class Singleton { } And I want to provide a singleton factory for it. I can do the (probably) obvious. I am not going to mention the enum possibility or any other, as they are of no interest to me. static final class SingletonFactory { private static volatile Singleton singleton; public static Singleton getSingleton() { if (singleton == null) { // volatile read synchronized (SingletonFactory.class) { if (singleton ==

double check locking without volatile (but with VarHandle release/acquire)

一个人想着一个人 提交于 2021-02-13 12:12:21
问题 The question is rather easy, in a way. Suppose I have this class: static class Singleton { } And I want to provide a singleton factory for it. I can do the (probably) obvious. I am not going to mention the enum possibility or any other, as they are of no interest to me. static final class SingletonFactory { private static volatile Singleton singleton; public static Singleton getSingleton() { if (singleton == null) { // volatile read synchronized (SingletonFactory.class) { if (singleton ==

Why enum singleton is lazy?

杀马特。学长 韩版系。学妹 提交于 2020-11-29 11:11:36
问题 I saw answers like these, tried to clarify via comments, and was unsatisfied by examples here. Maybe it's time for this specific question... Why enum singleton implementation is called lazy ? public enum EnumLazySingleton { INSTANCE; EnumLazySingleton() { System.out.println("constructing: " + this); } public static void touchClass() {} } How it is different from eager implementation? public class BasicEagerSingleton { private static final BasicEagerSingleton instance = new BasicEagerSingleton

Why enum singleton is lazy?

生来就可爱ヽ(ⅴ<●) 提交于 2020-11-29 11:09:34
问题 I saw answers like these, tried to clarify via comments, and was unsatisfied by examples here. Maybe it's time for this specific question... Why enum singleton implementation is called lazy ? public enum EnumLazySingleton { INSTANCE; EnumLazySingleton() { System.out.println("constructing: " + this); } public static void touchClass() {} } How it is different from eager implementation? public class BasicEagerSingleton { private static final BasicEagerSingleton instance = new BasicEagerSingleton

Does Java synchronized keyword flush the cache?

狂风中的少年 提交于 2020-01-14 08:20:11
问题 Java 5 and above only. Assume a multiprocessor shared-memory computer (you're probably using one right now). Here is a code for lazy initialization of a singleton: public final class MySingleton { private static MySingleton instance = null; private MySingleton() { } public static MySingleton getInstance() { if (instance == null) { synchronized (MySingleton.class) { if (instance == null) { instance = new MySingleton(); } } } return instance; } } Does instance have to be declared volatile in

Double checked locking in Android

青春壹個敷衍的年華 提交于 2020-01-11 17:08:14
问题 According to many, the somewhat common Double-Checked Locking idiom is broken for java unless you're running 1.5 or later and use the volatile keyword. A broken double-checked lock sample: // Broken multithreaded version // "Double-Checked Locking" idiom class Foo { private Helper helper = null; public Helper getHelper() { if (helper == null) synchronized(this) { if (helper == null) helper = new Helper(); } return helper; } // other functions and members... } The sample comes from this

What is the point of making the singleton instance volatile while using double lock? [duplicate]

寵の児 提交于 2020-01-09 06:01:08
问题 This question already has answers here : Why is volatile used in double checked locking (6 answers) Closed 2 years ago . private volatile static Singleton uniqueInstance In a singleton when using double lock method for synchronization why is the single instance declared as volatile ? Can I achieve the same functionality without declaring it as volatile ? 回答1: Without volatile the code doesn't work correctly with multiple threads. From Wikipedia's Double-checked locking: As of J2SE 5.0, this