Tuesday, January 31, 2012

Java Singleton - The Basics

There are number of ways to implement singleton in Java.
The very basic:
public class SingletonClass {
 
 private static final SingletonClass singleton;
 
 public static getSingletonInstance() {
  if (singleton == null) {
   singleton = new SingletonClass();
  }
  return singleton;
 }
}
However, using this way is not good if this is in a multi-threaded environment. The method should use the "synchronized" keyword to make it thread safe.
public static synchronized getSingletonInstance() {
 if (singleton == null) {
  singleton = new SingletonClass();
 }
 return singleton;
}
or
public static getSingletonInstance() {
 synchronized(SingletonClass.class) {
  if (singleton == null) {
   singleton = new SingletonClass();
  }
 }
 return singleton;
}
"synchronized" can be slowed because other threads will have to wait for it. It might not be a problem if only a few threads will be calling it at the same time. However, if there are a lot of threads trying to get the instance very frequently, then this should be a better approach:
public class SingletonClass {
 
 private static final SingletonClass singleton = new SingletonClass();
 public static getSingletonInstance() {
  return singleton;
 }
}
The shared instance is initialized in the static block so that there is no check in the method nor other threads need to wait for it. The drawback of this is that there is no lazy-load. The shared instance will be initialized even if the method is never called.
The thread-safe and lazy-load way from Bill Pugh:
public class SingletonClass {

        private SingletonClass() { 
  }
 
        private static class SingletonClassInstance { 
                public static final SingletonClassInstance instance = new SingletonClassInstance();
        }
 
        public static SingletonClass getInstance() {
                return SingletonClassInstance.instance;
        }
}
Thread-safe without the "synchronized" keyword and the instance is initialized on the first call. However, so far all of the above implementation needs to consider the serializable objects. If the fields of the singleton class are non-transient, attack can be done thru serializing and deserializing the SingletonClass.

Using enum to implement singleton base on Effective Java:
public enum SingletonClass {
 INSTANCE;
 
 public void doSomething() {
 }
}
Very clear implementation, guarantee against multiple instantiation, lazy-load, and no problem with the serializable objects like all of the above implementations. This should be so far the best way to implement Java singleton.

Monday, January 16, 2012

Putting code block in blogger posts - SyntaxHighlighter

Code block is important for writing technical blogs.

Since blogger doesn't have a default code block, I was trying to find a good way to put a code block for my posts.

I was first thinking to use the HTML5 <code></code> blocks and format them but I found the SyntaxHighlighter when I searched around. It is a self-contained javascript library for putting a nice formatted code blocks in webpages.

Here is how to do it in blogger:

1. Download or use the hosted version of SyntaxHighliger - I am using the hosted version since I don't want to host the files.

2. Go to the Templates setting in blogger and edit the template HTML. Right before the </head>, add:

  
  
  

3. The SyntaxHighligher comes with different kinds of brushes for different languages. Each brush type represent one type of programming lauguage block. I added the brushes using autoloader so that the page will only load the require script for the page instead of loading everything. When using the library in blogger, also need to set the bloggerMode as true. Following codes are added in the bottom of the template html (for my template, right below </macro:includable>):


4. Save Template. Now it is ready to use.

5. Start/Edit a post and switch to the HTML editor. To put a javascript code snipped:

function test() {
  //test
}

It will show up in the blog as:
function test() {
  //test
}

A list of brushes can be found here.