web developer & system programmer

coder . cl

ramblings and thoughts on programming...


singleton are not dangerous

published: 30-06-2011 / updated: 30-06-2011
posted in: development, java, programming, tips
by Daniel Molina Wegener

Yes, singleton patterns are not dangerous. That pattern is dangerous only for those developers that cannot handle concurrent programming or parallel programming, where you need to manage resources with concurrent access, even if they are read or write. There are some myths related to the singleton pattern, but they are just created by that kind of developers. If you don’t know about concurrent programming techniques, is obvious that your application is at risk if your are using singletons with the wrong implementation. Also it depends on how large is your application, how do you apply governance over you application design and which platform is using.

Certainly an application that uses a worker model, using threads, probably requires concurrent programming techniques to use the singleton pattern. That kind of applications, for example those implemented under JavaEE containers, like IBM WebSphere and Oracle WebLogic, require a good governance over the code and a good knowledge on how will be used a singleton implementation. In other environments, where static objects are not shared on the stack, you don’t need to care about singleton implementations, like PHP environments, where only one thread or process is reaching the singleton object, it just requires to be instanced once, and do not requires mutexes.

If you are good enough, you can use the singleton pattern on worker enabled environments. The well known getInstance method, should be implemented as follows in Java under JavaEE environments:

class SingletonSampleImpl extends Object {

    private static Object singletonMutex = new Object();
    private static SingletonSampleImpl instance = null;

    public static SingletonSampleImpl getInstance() {
        synchronized (singletonMutex) {
            if (instance == null) {
                instance = new SingletonSampleImpl();
            }
        }
        return instance;
    }
}

In other environments like PHP, you do not need to care about creating a mutex that protects the singleton instance to be overwritten or allocated twice, you just need to care about an object instance that is not accessed twice by various threads concurrently.

class SingletonSample {

    private static $instance = null;

    public static function getInstance() {
        if ($instance == null) {
            $instance = SingletonSample();
        }
        return $instance;
    }
}

Also you must be very careful, and never externalise the control of mutexes to external routines. A mutex must be controlled locally. The reason is simple, you just need to avoid a deadlock by double locking the mutex. If you externalise the control of a mutex, you are in risk of reaching a situation like the following problem, where the second lock will wait for the first one, creating a deadlock and killing the thread.

synchronized (singletonMutex) {
    synchronized (singletonMutex) {
        instance = new SingletonSampleImpl();
    }
}

This is just the situation that you can reach by externalising the control of a mutex, not the algorithm itself that you will implement, but the risk that you run by allowing mutexes to be locked outside of the controlling class. This is a similar situation that generates a race condition when you are programming singletons in worker environments without using the proper mutex. The wrong implementation of a singleton that is under the risk of a race condition is on the following example, where the algorithm just checks for a null value, and do not uses a lock for the concurrent threads that can instantiate the instance object.

class SingletonSampleImpl extends Object {

    private static Object singletonMutex = new Object();
    private static SingletonSampleImpl instance = null;

    public static SingletonSampleImpl getInstance() {
        if (instance == null) {
            instance = new SingletonSampleImpl();
        }
        return instance;
    }
}

So, using singletons is not dangerous. Just requires to have developers with the proper knowledge.


one comment to “singleton are not dangerous”

  1. Strictly speaking with the current Zend PHP 5 Runtime, your singleton instance will never be accessed concurrently.

post a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>