JAN*_*JAN 0 java singleton multithreading thread-safety
考虑Singleton:
public final class MySingleton {
private static class Nested
{
private static final MySingleton INSTANCE = new MySingleton();
}
private MySingleton()
{
if (Nested.INSTANCE != null)
{
throw new IllegalStateException("Already instantiated");
}
}
public static MySingleton getInstance()
{
return Nested.INSTANCE;
}
}
Run Code Online (Sandbox Code Playgroud)
我没有放任何锁,但为什么这是Singleton问题的线程安全解决方案?
| 归档时间: |
|
| 查看次数: |
69 次 |
| 最近记录: |