Thursday, 8 October 2015

Single Design Pattern with Double locked checking...........

import java.io.Serializable;

public class LazyBizService implements Serializable {

private volatile static LazyBizService bizService;

private LazyBizService() {
System.out.println("____BizService is instantiated___");
}

static public LazyBizService getInstance() {
if(bizService==null) {
   //synchronized block
   synchronized (LazyBizService.class) {
    if(bizService==null) {
      bizService=new LazyBizService();
    }  
}
}
  return bizService;
}

// This will fix the de-serialization issue
    private Object readResolve() {
         // Return the available instance instead.
         return bizService;         
    }
}

No comments:

Post a Comment