Singleton and Double-Checking Locking
Singleton with single thread
Use static instance variable
Use private constructor
use static getInstance method

This version of Singleton only works on single thread since getInstance method is not protected by any synchronization
Singleton with multiple threads [slow version]
Use synchronized in getInstance

It is slow since synchronization is only necessary when the object is initialized
After the object is initialized, the object is NOT NULL anymore; hence, synchronization block is unnecessary
Singleton with multiple threads [fast version]
Double-Checking Locking technic
Use synchronized inside getInstance and check the instance object is NULL or NOT
class Stack{
        int maxLen;
        int count;
        char* array;
        Stack(int max){
            count = 0;
            maxLen = max;
            array = new char[maxLen];
        void push(char ch){
            if(count < maxLen){
                array[count] = ch;
        char pop(){
            char ch;
            if(count > 0){
                ch = array[count-1];
            return ch;
        bool isEmpty(){
            return count == 0;
        int size(){
            return count;
                delete[] array;