可能重复:
静态类和单例模式之间的区别?
哪个在Java中更好,
实现公共静态方法,比如
Factory.createLoginRequest()
Run Code Online (Sandbox Code Playgroud)
或者实现Singleton模式,比如
Factory.getInstance().createLoginRequest()
Run Code Online (Sandbox Code Playgroud)
(Boths将返回一个Request对象.)
哪一个更好,为什么?
我正在思考编写单例类的其他方法.那么这个类被认为是一个单例类吗?
public class MyClass{
static Myclass myclass;
static { myclass = new MyClass();}
private MyClass(){}
public static MyClass getInstance()
{
return myclass;
}
}
Run Code Online (Sandbox Code Playgroud)
因为静态块只运行一次.
class MyClass
{
private static volatile Resource resource;
public static Resource getInstance()
{
if(resource == null)
resource = new Resource();
return resource;
}
}
Run Code Online (Sandbox Code Playgroud)
这里我怀疑是在实践中根据java并发性,如果你使用volatile,安全发布会发生(即一旦引用对另一个线程可见,数据也可用).我可以在这里使用吗?但如果它是正确的,那么假设thread1现在检查"资源"并且它为null,因此它开始创建对象.当thread1正在创建objet时,另一个线程即thread2开始检查"resource"的值,而thread2发现它为null(假设创建"resource"对象需要相当长的时间,而且thread1尚未完成创建,所以安全发布没有发生因此不可用于thread2)那么它还会开始创建对象吗?如果是,那么类不变量就会中断.我对么?请帮我理解这里特别使用的volatile.
阅读本网站,我发现了这个:
[]行
private static final Foo INSTANCE = new Foo();只在实际使用类时执行,它负责延迟实例化,并保证线程安全.
为什么这保证是线程安全的?因为这个领域是最终的?还是出于其他原因?
您知道,自Java 5发布以来,在Java中编写Singleton模式的推荐方法是使用枚举.
public enum Singleton {
INSTANCE;
}
Run Code Online (Sandbox Code Playgroud)
但是,我不喜欢这个 - 是强制客户端使用Singleton.INSTANCE来访问单例实例.也许,更好的方法是在普通类中隐藏Singleton,并提供更好的单例设施访问:
public class ApplicationSingleton {
private static enum Singleton {
INSTANCE;
private ResourceBundle bundle;
private Singleton() {
System.out.println("Singleton instance is created: " +
System.currentTimeMillis());
bundle = ResourceBundle.getBundle("application");
}
private ResourceBundle getResourceBundle() {
return bundle;
}
private String getResourceAsString(String name) {
return bundle.getString(name);
}
};
private ApplicationSingleton() {}
public static ResourceBundle getResourceBundle() {
return Singleton.INSTANCE.getResourceBundle();
}
public static String getResourceAsString(String name) {
return Singleton.INSTANCE.getResourceAsString(name);
}
}
Run Code Online (Sandbox Code Playgroud)
所以,客户现在可以简单地写:
ApplicationSingleton.getResourceAsString("application.name")
Run Code Online (Sandbox Code Playgroud)
例如.哪个好多了:
Singleton.INSTANCE.getResourceAsString("application.name")
Run Code Online (Sandbox Code Playgroud)
所以,问题是:这是正确的方法吗?此代码是否有任何问题(线程安全?)?它具有"enum …
在Scala中,我们可以写
object Foo { def bar = {} }
Run Code Online (Sandbox Code Playgroud)
这是如何由编译器实现的?我能够Foo.bar();从Java 调用,但是new Foo();从Java中调用错误cannot find symbol symbol: constructor Foo()
注意:这是输出的代码 scalac -print
package <empty> {
final class Foo extends java.lang.Object with ScalaObject {
def bar(): Unit = ();
def this(): object Foo = {
Foo.super.this();
()
}
}
}
Run Code Online (Sandbox Code Playgroud) 一个共同的(1,2)实现单的方式是使用具有静态构件的内部类:
public class Singleton {
private static class SingletonHolder {
public static final Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
private Singleton() {
//...
}
}
Run Code Online (Sandbox Code Playgroud)
据说这个实现是懒惰的初始化和线程安全的.但究竟是什么保证了它的线程安全?处理 线程和锁定的JLS 17没有提到静态字段具有任何类型的先发生关系.我怎样才能确定初始化只发生一次并且所有线程都看到同一个实例?
我正在阅读这篇关于"双重检查锁定"的文章,并且在文章的主题之外我想知道为什么在文章的某些方面作者使用下一个成语:
清单7.尝试解决乱序写入问题
Run Code Online (Sandbox Code Playgroud)public static Singleton getInstance() { if (instance == null) { synchronized(Singleton.class) { //1 Singleton inst = instance; //2 if (inst == null) { synchronized(Singleton.class) { //3 inst = new Singleton(); //4 } instance = inst; //5 } } } return instance; }
我的问题是:有没有理由用同一个锁同步两次代码?有这个任何目的吗?
提前谢谢了.
在阅读了关于枚举的一些问题和答案后,我发现它并没有真正有用......
它是类和变量之间的东西,但我知道在哪里可以使用它,所以它比一个类或一些变量更有用.
可能重复:
在Java中实现单例模式的有效方法
我正在阅读这篇关于Java的最佳单例实现,但它不是线程安全的.
根据维基:
if(singleton==null) { synchronized(Singleton.class) { // this is needed if two threads are waiting at the monitor at the // time when singleton was getting instantiated if(singleton==null) singleton= new Singleton(); }
}
但是Find Bugs实用程序在此中给出了两个错误:1.双重空检查.2.静态字段的延迟初始化不正确.
什么是最好的方法,
这是对的吗 :
Run Code Online (Sandbox Code Playgroud)synchronized (Singleton.class) { if (singleton== null) { singleton= new Singleton(); } }
java ×10
singleton ×4
enums ×2
android ×1
concurrency ×1
locking ×1
scala ×1
static ×1
synchronized ×1