Well I have seen this Singleton story around here (and already on the
web) for a while now, and I will try to shed some light to this:
1.
A Singleton is a pattern used to ensure a class has only one instance
and to provide a global point of access to it.
(If thats not detailed enough I recommend to refer to my Patterns bible:
Design Patterns, Elements of Reuseable Object Oriented Software
Gamma,Helm,Johnson,Vlissides [Addison-Wesley]
ISBN0-201-63361-2, which I found to be a very good handbook for Patterns)
2.
In Java there are different approaches which would actually fit this pattern.
One is to use a class with only static methods and static members.
This represents a singleton (just one instance, global point of access).
The second one would be a class holding a static reference to its
only instance and exposing a static method that returns exactly this
instance, or a newly created in case of the first call on the method.
Plus the constructor might/should throw an Exception, if any form of
further instantiation is tried.
A source might demonstrate this:
public class MySingleton {
//static encapsulated single instance
private static Object Self=null;
public MySingleton() throws InstantiationException {
if(Self==null) {
Self=this;
} else {
throw new InstantiationException("Singletons"+
+" can only instantiated once.");
}
}//constructor
public static MySingleton getInstance() {
if(Self==null) {
try {
return new MySingleton();
} catch (InstantiationException ex) {
//should not happen, maybe panic ;)
return null;
}
} else {
return (MySingleton)Self;
}
}//getInstance
}//class MySingleton
Now because I wanted to ensure that this works I created a small
test class, that should verify if the MySingleton survives a gc; and if run
with java -verbosegc it will show it does (even with jdk1.1.7 and
ibm-jdk 1.1.8 under Linux):
public class singtest {
public static void main(String[] args) {
try {
System.out.println("Testing MySingleton...");
System.out.println("Factorized instance:"
+ MySingleton.getInstance().toString());
System.out.println("Trying Instantiation (should throw Exception)");
try {
MySingleton single=new MySingleton();
} catch (SingletonException sx) {
sx.printStackTrace();
}
System.out.println("Using more times....");
for(int i=0;i<5;i++){
System.out.println("Instance:"+Singleton.getInstance().toString());
}
//now I make a thread, so that gc can do its work for sure
//because calling gc is async.
System.out.println("Running garbage collect test....");
Thread mytest=new Thread();
mytest.start();
while(true) {
System.gc();
mytest.sleep(30000);
System.out.println("Instance:"+Singleton.getInstance().toString());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}//main
}//class singtest
Well there is no Reference stored in the singtest class
and MySingleton is neither garbage collected nor any new instantiation
occurs.
>So I have been noticing so very wierd behavior in
>Cocoon/Jetspeed/Turbine under JDK 1.1 (Specifically the Blackdown JDK
>1.1.8 and the IBM JDK 1.1.8, both on Linux).
>
>Basically singletons don't work. Both VMs will GC static members right
>after there are no references. For Servlet code that can happen very
>quickly. With finalize methods you can clearly see this happening.
Better debug it with -verbosegc this will show you if the instances were really
collected, finalizers are such a thing....
>Everythign works just fine with the Singleton. But when the you access
>the Singleton again it is re-instantiated for you by the VM. Totally
>getting rid of the advantage of a singleton.
I guess that the implementation has an unwanted "refresh feature" ;)
(i.e.a bug maybe?)
> >From everything I have ever read it isn't supposed to work this way. It
>is a static method and is supposed to be global to a specific name of a
>class and not an instantiation.
>
>There is a kind of a kludge work around. You put your instance member
>within the System properties via:
>
>System.getProperties().put()
>
>The only problem is that java.util.Properties throws a
>ClassCastException when you try to enumerate all your props (stupid
>SUN!).
>
>Therefore I don't want to do this.
>
>Ug.
>
>--
>Kevin A Burton ([EMAIL PROTECTED])
>http://relativity.yi.org
>Message to SUN: "Please Open Source Java!"
>"For evil to win is for good men to do nothing."
>
>
>------------------------------------------------------------
>To subscribe: [EMAIL PROTECTED]
>To unsubscribe: [EMAIL PROTECTED]
>Problems?: [EMAIL PROTECTED]
Hope that helped,
Regards
Dieter Wimberger
------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Problems?: [EMAIL PROTECTED]