对于单例模式,使用双重检查锁定习惯是否更好?还是同步方法?
即:
private static volatile ProcessManager singleton = null;
public static ProcessManager getInstance() throws Exception {
if (singleton == null) {
synchronized (MyClass.class) {
if (singleton == null) {
singleton = new ProcessManager();
}
}
}
return singleton;
Run Code Online (Sandbox Code Playgroud)
}
要么
private static processManager singleton = null;
public synchronized static processManager getInsatnce() throws Exception {
if(singleton == null) {
singleton = new processManager();
}
return singleton
}
Run Code Online (Sandbox Code Playgroud) 在多个类加载器中使用时,我遇到了我的单例问题.例如,Singleton由多个EJB访问.有没有办法创建一个在所有类加载器中只有一个实例的单例?
我正在寻找使用自定义类加载器或其他方式的纯java解决方案.
我只需要在某处阅读以下代码:
public class SingletonObjectDemo {
private static SingletonObjectDemo singletonObject;
// Note that the constructor is private
private SingletonObjectDemo() {
// Optional Code
}
public static SingletonObjectDemo getSingletonObject() {
if (singletonObject == null) {
singletonObject = new SingletonObjectDemo();
}
return singletonObject;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要知道这部分需要什么:
if (singletonObject == null) {
singletonObject = new SingletonObjectDemo();
}
Run Code Online (Sandbox Code Playgroud)
如果我们不使用这部分代码怎么办?仍然会有一个副本SingletonObjectDemo,为什么我们需要这个代码呢?
我抓住了一些问题 - 我可能会对此完全错误.
我正在尝试创建一个扩展ArrayList的类,但有几个方法可以增加功能(至少对于我正在开发的程序而言).
其中一个方法是findById(int id),它在每个ArrayList对象中搜索特定的id匹配.到目前为止,它正在发挥作用,但它不会让我这样做for (Item i : this) { i.getId(); }
我不明白为什么?
完整代码:
public class CustomArrayList<Item> extends ArrayList<Item> {
// declare singleton instance
protected static CustomArrayList instance;
// private constructor
private CustomArrayList(){
// do nothing
}
// get instance of class - singleton
public static CustomArrayList getInstance(){
if (instance == null){
instance = new CustomArrayList();
}
return instance;
}
public Item findById(int id){
Item item = null;
for (Item i : this) {
if (i.getId() == id) …Run Code Online (Sandbox Code Playgroud) 在我上次的采访中,我被问到一个关于java中所有Singleton实现的标准问题.他们都有多糟糕.
我被告知,即使静态初始化也很糟糕,因为构造函数中出现未经检查的异常的可能性:
public class Singleton {
private static Singleton instance = new Singleton();
public static Singleton getInstance() {
return instance;
}
private Singleton() {
throw new RuntimeException("Wow... Exception in Singleton constructor...");
}
}
Run Code Online (Sandbox Code Playgroud)
他们还告诉我,Exception将是"ClassNotFoundException",因此在真正的应用程序中找到问题非常困难.
我试图得到那个例外:
public static void main(String[] args) {
new Thread(new Runnable(){
public void run() {
Singleton.getInstance();
}
}).start();
}
Run Code Online (Sandbox Code Playgroud)
但我唯一得到的是ExceptionInInitializerError ......
我搜索了这个例外以及我发现的所有地方 - 他们都谈到了我在采访中被告知的同样问题.没有关于"实施"=)
感谢您的关注.
我正在研究在java中执行单例的正确方法的代码:在Java中实现单例模式的有效方法是什么?
我有点困惑,你如何在枚举中添加一个方法?
public enum Elvis {
INSTANCE;
private final String[] favoriteSongs =
{ "Hound Dog", "Heartbreak Hotel" };
public void printFavorites() {
System.out.println(Arrays.toString(favoriteSongs));
}
}
Run Code Online (Sandbox Code Playgroud)
上面代码中的枚举对我来说甚至没有意义,你有符号:
INSTANCE;
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
我来自c#,这种语法让我很好奇,我希望有人可以解释或理解上述内容.
我想这意味着java有一个更纯粹的枚举概念,因为它可以有行为?
在处理项目时,我遇到了一项任务,即设计一组实现定义简单操作的接口的类.通常这些类会按特定顺序完成它们的工作,但是同时只能从其中一个调用方法的可能性也是必需的.
考虑到以上所有因素并考虑到: - 每个类都有相当基本的逻辑 - 不需要扩展另一个类 - 将所有类放在一个文件中可能很方便 - 在需要时编辑源文件不是问题
我提出了以下解决方案(实际课程不是那么做作,但下面的例子足以给你一些基本的想法):
public enum Bestiary {
DOG(1) {
@Override
void makeNoise(Loudspeaker ls) {
ls.shoutOutLoud("I am alpha dog");
}
},
CAT(2) {
@Override
void makeNoise(Loudspeaker ls) {
ls.shoutOutLoud("I am beta cat");
}
},
RAT(3) {
List<String> foods = new ArrayList<>();
{
foods.add("gods");
foods.add("dogs");
foods.add("cats");
foods.add("other rats");
}
@Override
void makeNoise(Loudspeaker ls) {
StringBuilder cry = new StringBuilder("I am THE rat; usually I eat ");
for (int i = 0; i < foods.size(); …Run Code Online (Sandbox Code Playgroud) 当两个线程同时调用"getInstance()"时,Singleton如何表现?保护它的最佳做法是什么?
将Singleton的实例声明为static或更好static final?
请参阅以下示例:
static 版
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
static final 版
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return INSTANCE;
}
}
Run Code Online (Sandbox Code Playgroud) 我看到了这样的答案,试图通过评论来澄清,但对这里的例子不满意。
也许是时候提出这个具体问题了......
为什么枚举单例实现被称为惰性?
public enum EnumLazySingleton {
INSTANCE;
EnumLazySingleton() {
System.out.println("constructing: " + this);
}
public static void touchClass() {}
}
Run Code Online (Sandbox Code Playgroud)
它与急切的实现有何不同?
public class BasicEagerSingleton {
private static final BasicEagerSingleton instance = new BasicEagerSingleton();
public static BasicEagerSingleton getInstance() {
return instance;
}
private BasicEagerSingleton() {
System.out.println("constructing: " + this);
}
public static void touchClass() {}
}
Run Code Online (Sandbox Code Playgroud)
两者都将初始化实例而不访问INSTANCE/getInstance()- 例如 call touchClass()。
public class TestSingleton {
public static void main(String... args) …Run Code Online (Sandbox Code Playgroud) java singleton enums double-checked-locking lazy-initialization