If the construction of the singleton object is expensive, then you may consider using SoftReference instead of WeakReference.
解决方案 »
- 请教一个关于svn的问题
- 解析xml文件的奇怪问题
- 两个线程为什么不交替执行?
- jdk1.5是不是不需要配置环境变量了?
- SimpleTimeZone.getDefault().inDaylightTime(Date d)的参数是何意思
- 初学java。。。求助!!!
- 东北大学东软信息技术学院,大家知不知道里面的具体情况,你朋友在那里上学吗,小弟今年想参加那里的专升本考试,帮小弟介绍介绍吧,谢谢
- 加急!!给高分的,关于Java的Jdbc数据库读取操作的Applet程序!!!
- 怎样学习JAVA语言?
- 数据库连接,100相送!!!!!!!!!!!!!
- 请教各位大虾:利用asp怎么把数据转化成word文档和pdf文件!
- 一个RandomAccess中的point问题
private Singleton (){}
static final Singleton _inst=new Singleton();
static I getInstance(){return _inst;}
//other stuffs
}
And, when you load a class with a method like
void f(Singleton s){......}
it'll load class Singleton. even if you never call Singleton.getInstance(), I believe that static variable will still be initialized.
class Singleton implements I{
private Singleton (){}
static final Singleton _inst=new Singleton();
static I getInstance(){return _inst;}
//other stuffs
}is preferable in most of the cases.
don't always "if(_inst==null)......" because you see this on some book.
if considering concurrency, it's even worse. but there's a very effecient way called double checking idiom. you can take a look at the Singleton template for it's implementation. search ACE (The ADAPTIVE Communication Environment) in google to find the url.
Thank you for up it. :)But anyway, this is not for C++. And of course you have to write your singleton as order-independent. That's not an issue in many cases.
So if you don't need the lazy initialization, just keep it simple. Later, if you do need it because of the runtime configuration or any other thing, just change it.btw, thread safety/performance is one drawback of the lazy init approach, not an advantage.
:)
I searched and found the web site, but did not find what you were talking about.
{
lock();
if(singleton_instance==null)
{
create singleton;
}
}
else
{
return singleton_instance;
}
the first check only failed if singleton_instance never created. after that, you never need to lock.
first of all, this approach makes the code even more complex.secondly, you'll still have to call if(instance==null) each time.third, this double check does not work if we use the WeakReference or SoftReference.And, the static initialized approach just does not have concurrency problem at all.
http://www.cs.wustl.edu/~schmidt/ACE-papers.html for a general view of ACE.
The double check can also work for WeakReference and SoftReference.
The only thing is it could be quite complex.But anyway, it's a very good idea when we do need both lazy init and thread-safety!
mm, talking about design patterns, but it's purpose is compare with c++.
i think both ok if we don't care about the memory.
but my prefer the complex codes.^_^
mm, talking about design patterns, but it's purpose is compare with c++.
i think both ok if we don't care about the memory.
but my prefer the complex codes.^_^
code like that:
public static Singleton getInstance() {
/* in a non-thread-safe version of a Singleton */
/* the following line could be executed, and the */
/* thread could be immediately swapped out */
if (instance_ == null) {
synchronized(this) {
if (instance_ == null) {
instance_ = new Singleton();
}
}
}
return instance_;
}in my mind, i think it's work fine.hehe, but i have seen an article:
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.htmltake a look at it. maybe it's true.
public class JGKSingleton {
/* Here is the instance of the Singleton */
private static JGKSingleton instance_;
/* Need the following object to synchronize */
/* a block */
private static Object syncObject_;
/* Prevent direct access to the constructor
private JGKSingleton() {
super();
}
public static JGKSingleton getInstance() {
/* in a non-thread-safe version of a Singleton */
/* the following line could be executed, and the */
/* thread could be immediately swapped out */
if (instance_ == null) {
synchronized(syncObject_) {
if (instance_ == null) {
instance_ = new JGKSingleton();
}
}
}
return instance_;
}
}in my mind, it's work fine.
hehe, but i suspect it since i saw this article:
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.htmlmaybe it's true.
i'll check it.
http://www.javaworld.com/javaworld/jw-01-2001/jw-0112-singleton.htmllet's read it.^_^
thought the double check works!anyway, it proves my claim: never bother complex code if not necessary.
Thank you yakuu! It has been extremely helpful! Otherwise, I might use the double check in my future projects.
:)
anyone knows if the memory model fixed?
If not, then java on mp machines are broken. with immutable can be changed, your program are bond to break.
private static JGKSingleton instance_;
private static Object syncObject_;
public static JGKSingleton getInstance() {
if (instance_ == null) {
synchronized(syncObject_) {
if (instance_ == null) {
instance_ = new JGKSingleton();
}
}
}
return instance_;
}
}the new code:public class JGKSingleton {
private static volatile JGKSingleton instance_;
private static Object syncObject_;
public static JGKSingleton getInstance() {
if (instance_ == null) {
synchronized(syncObject_) {
if (instance_ == null) {
instance_ = new JGKSingleton();
}
}
}
return instance_;
}
}
Now, volatile does not help at all.
But, don't think many Java code is "bound to" break.
Basically, the language and VM specifications don't really make it clear/precise/etc. what should/shouldn't really be done in the presence of volatile and so many implementations of the JVM don't do anything if volatile is specified. Note that there are some folks actively working to solve this in time for the next release.so, we still rely upon the double check code.
The problem with double check was not ever promised by the jls that it will work.
So, if any program using double check failed, it's the code's bug,(at most you can blame the jls is not clear or not good) just as many other bugs in concurrent code. They are normally hard to find in nature.
and how many programs are using double check? don't think it's quite useful in real. As the article said, "static init" is neat and good enough.
"volatile" may be used to fix the problem in double check in the future. But before the new jls is made and all the current impl supports that, we as programmers should just wisely avoid double check.Again, strongly recommend using "static init". Easy! most efficient! robust!
continued topic is far off.
but can we talk about the design patterns.
i can use 200 each topic.^_^
design patterns can easily go too abstract and phylosophical.