0

I was going through design pattern and came across with Singleton Pattern

class SingletonPattern implements Runnable {
    private static SingletonPattern single=null;

    private SingletonPattern() { }

    public synchronized static SingletonPattern getInstance() {
        if(null==single) {
            single=new SingletonPattern();
        }
        return single;
    }
}

Now I understand that synchronized will help that two thread cannot access the getInstance method but correct me if I am wrong two different object will have two locks each having one.Another thread can be started from anther object and get then access the getInstance() method thus we can have two objects.??

Esko
  • 29,022
  • 11
  • 55
  • 82
Deepak_Sharma
  • 61
  • 2
  • 10
  • 1
    Lock is acquired on the type not object here . – AllTooSir Jul 20 '13 at 07:57
  • 1
    its `static synchronized` method, so lock will be acquired on `class` object of SingletonPattern class. check this thread http://stackoverflow.com/questions/437620/java-synchronized-static-methods-lock-on-object-or-class – sanbhat Jul 20 '13 at 08:10

4 Answers4

2

No. The synchronized method will prevent 2 threads from simultaneously calling the method. You can read up on synchronized here. In case of a static method, the synchronized acts on the class rather than object.

However, this way of making Singletons is inefficient. And Double Checked Locking is broken. The best way to do singletons in java is by using a Enum

user93353
  • 13,733
  • 8
  • 60
  • 122
  • Ok Then does Singleton Pattern says that during a particular period there can be only one instance of a class – Deepak_Sharma Jul 20 '13 at 08:01
  • @Deepak_Sharma - you constructor is private - so nobody can instantiate your class except for the getInstance method. – user93353 Jul 20 '13 at 08:02
  • The double check idiom is broken, but why should synchronized around the method not work correctly? All Thread caches are updated correctly before entering and flushed after leaving. ??? – morpheus05 Jul 20 '13 at 08:07
  • @Deepak_Sharma - and because the method is static, the synchronized acts on the class rather than the object. – user93353 Jul 20 '13 at 08:13
  • Wat if i dont make the constructor private – Deepak_Sharma Jul 20 '13 at 08:15
  • 1
    @Deepak_Sharma - If the ctor is not private, then people don't need to call `getInstance` at all - they will instantiate objects normally - `Singleton single = new Singleton(); Singleton anothersingle = new Singleton(); Singleton yetanothersingle = new Singleton();`. Making the `ctor` privates forces people to call `getInstance` to get an object & that's where you get the Singleton pattern. – user93353 Jul 20 '13 at 08:35
  • @Deepak_Sharma - If you found any of the answers useful and solved your problem you should consider accepting an answer - http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – user93353 Jul 20 '13 at 17:51
0

The variable single is static. That is to say, all instance of class SingletonPattern share the same variable single. The first time the function getInstance() execute, the variable single is null, so

single = new SingletonPattern();

executes, which makes variable single not null any more.

Then all the successive call of function getInstance() would not enter the if clause, thus only return the same variable single, which is a reference to the same instance of the class SingletonPattern.

Also the synchronized keyword makes sure the function getINstance() would not be called in two threads at the same time.

lulyon
  • 6,707
  • 7
  • 32
  • 49
0

No. Since getInstance and single are static both threads will use the very same method and object so there won't be two objects. synchronized will ensure that they won't access simultaneously inside getInstance

Mehmet Ataş
  • 11,081
  • 6
  • 51
  • 78
0

synchronized static method will acquire Class lock and there is a single class lock available for all objects of this class. Hence different objects will be prevented from acquiring this lock at the same time.

But this mechanism works as long as different classloaders are not involved.

Although best way to implement singletons in java is to use enums

Mangoose
  • 922
  • 1
  • 9
  • 17