Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

singleton

Thread view: 
swan2030@gmail.com - 22 Sep 2006 17:46 GMT
I saw two ways of instantiate singleton.
Somebody can help me compare those two. ( with multi-thread in mind).

1)
private static final MyClass singleton = new MyClass();
public static MyClass getInstance()
{
 return singleton;
}

2)   Lazy and thread safe
static private class Holder
{
static protected final MyClass instance= new MyCLass();
}

public static MyClass getInstance()
{
 return MyClass.Holder.instance;
}

Thanks,
Thomas Fritsch - 22 Sep 2006 18:33 GMT
> I saw two ways of instantiate singleton.
> Somebody can help me compare those two. ( with multi-thread in mind).
[quoted text clipped - 16 lines]
>   return MyClass.Holder.instance;
> }

3)  Lazy and thread-safe
private static MyClass singleton;
public synchronized static MyClass getInstance()
{
  if (singleton == null)
    singleton = new MyClass();
  return singleton;
}

IMHO all 3 implementations are equivalent with respect to thread-safety.
But they are different with respect to memory/time-consumption.

(1) has the advantage of being simple.
And, by the way, it is thread-safe, too.
It has the disadvantage of creating the MyClass instance, even if it is
never needed. This may be an issue, if the MyClass constructor loads a
lot of classes or does other memory/time-consuming actions.

(2) and (3) have the advantage of creating the MyClass instance lazy,
i.e. as late as possible. This may save some memory/time, if the
getInstance() method is never called.

Signature

Thomas

alexandre_paterson@yahoo.fr - 22 Sep 2006 19:14 GMT
...
> > 2)   Lazy and thread safe
> > static private class Holder
[quoted text clipped - 15 lines]
>    return singleton;
> }

To be more complete...

The whole point of (2) is to have lazy instantation, being
thread-safe *and* avoid the cost of mutual exclusion.

In (3) you're not avoiding that cost, due to the "synchronized".

(2) is basically the construct recommended by Joshua Bloch
in Effective Java when you really need to have lazy instantiation,
thread-safety and want to avoid synchronization.

Not that in practice it matters that much or anything ;)

 Alex


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.