3

I am using a singleton created by the initialization-on-demand holder idiom. When I´m done, I would like to "return" the singleton so that it can be used by other programs. Can I do this as follows...

Creating the Singleton

public class MyObject{
    private MyObject(){}
    private static class Holder{
       private static final MyObject INSTANCE = new MyObject();
    }
    public static MyObject getInstance(){
       return Holder.INSTANCE;
    }
}

somewhere else I use this by

MyObject myObject = MyObject.getInstance();
// do something
myObject = null;
System.gc();
tip
  • 85
  • 1
  • 5
  • I dont understand why you want to destroy your singleton instance? – Lrrr Sep 20 '14 at 10:20
  • May I suggest using [an enum](http://stackoverflow.com/questions/70689/) instead? – fredoverflow Sep 20 '14 at 10:24
  • i have two threads running, each of them accessing the MyObject instance from time to time. I`m afraid, if I use it in one thread and not releasing it I cannot access it from the other thread. Or will it just take my instance away from the thread where it is in use? – tip Sep 20 '14 at 10:25
  • 1
    No, it would be the same instance used by both threads. What I think you need is the [Object pool pattern](http://en.wikipedia.org/wiki/Object_pool_pattern). An object managing one or more instances of a class, from which you can borrow an instance to be returned later. – Newerth Sep 20 '14 at 10:51
  • @tip Why do you want to prevent your singleton to be used by two threads? It might help if you give more context to your problem, as people are currently guessing the best solution while feeling that some key information is missing. So far, it does sound like you need mutual exclusion, as indicated by james large. – jlr Sep 20 '14 at 15:24

2 Answers2

2

This accomplishes nothing:

myObject = null;

The singleton object will always be referenced by the final INSTANCE field in the Holder class, and it never will be GCd.

In fact, that's what "singleton" means. A "singleton" is a class for which only one instance is ever created, and the instance is never destroyed. If you want something else, then call it something else. Don't call it "singleton."

I bet you either want to use some form of mutual exclusion to prevent more than one thread from using the singleton at the same time, or else you want what @Newerth said: an object pool.


Also, this is obsolete: System.gc(); In the very early days of Java, it would bring everything else to a halt while the GC reclaimed all of the unused objects from the heap, but modern JVMs do continuous garbage collection in the background. The documentation for System.gc() has been changed to say that it's only a suggestion.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
1

Yes that will work, but not sure why you need a holder class since your initialization is not costly (yet). If you are going to add more to the constructor later then fine, but otherwise it just clutters the code to have that other class.

nmore
  • 2,474
  • 1
  • 14
  • 21