Friday, June 25, 2010

Java singletons and initialization-on-demand

Ah, Effective Java, I love you so.  A useful tip from the book that I use often is the initialization-on-demand holder class for singletons.

Note that a simple and thread-safe way to create a singleton is:

public class SimpleSingleton {
    private SimpleSingleton() {}

    private static SimpleSingleton instance = new SimpleSingleton();

    public static SimpleSingleton getInstance() {
        return instance;
    }
}


The first reference to SimpleSingleton will initialize the static instance field, and the JVM will take care of synchronization if there is a race condition in invoking getInstance().

If early initialization of the singleton is not a performance concern (which is probably the majority of cases), then the above snippet suffices.  However, there are scenarios under which the instance member variable may be initialized prior to invoking the getInstance() method, so if you are really concerned about only initializing if necessary, then a different approach is needed.  

One route is to synchronize the getInstance method, but that creates overhead on each call.

An elegant solution is to create a private inner class as an initialization-on-demand holder:

public class SimpleSingleton {
    private SimpleSingleton() {}

    private static class Holder {
        private static SimpleSingleton instance = new SimpleSingleton();
    }

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

The only reference to the Holder inner class occurs in the getInstance method, so it is guaranteed that initialization will only happen during the method's first invocation.

The holder pattern is simple and readable enough that I use it by default on all of my singleton implementations.

Thursday, June 24, 2010

Energy efficiency in appliances... how important is it?

My first instinct in shopping for appliances is to look for ones that are the most energy efficient, more in order to save money than for any environmental reasons.  However, if you live in a cold climate, how important is it to cut your electricity usage, when all the juice is going to ultimately end up as heat anyway?  Granted, it makes a difference in the summer time, when your A/C will have to pump all that heat out.  But in the winter, your furnace will have to work less while your old fridge merrily chews up watts.

Based on a few minutes' Googling, the average home furnace is rated at 75000 BTU, which is roughly 20 kW.  Usage must vary a lot with the outside temperature and how well a home is insulated, but let's guess that a furnace is on for about 10% of the time in the middle of winter, leading to an average of around 2 kW.   A modern refrigerator will draw upwards of 70 W, so your major appliances can throw off a significant amount of heat relative to the furnace.  You'll certainly see detailed postings on the internet about how people's electric bill went down when they bought an efficient new fridge, but did they look at their gas bill, too?

Hola

I'll mostly be writing about coding, physics, numbers and other such nerdiness.  Enjoy!