标签:
在web应用中服务器面临的是大量的访问请求,免不了多线程程序,但是有时候,我们希望在多线程应用中的某一个类只能新建一个对象的时候,就会遇到问题。
class singleton{ private singleton(){ //..... } private static singleton instance; public static singleton getinstance(){ if(instance==null) //1 instance = new singleton(); //2 return instance } }
class singleton{ private singleton(){ //..... } private static singleton instance; public static singleton synchronized getinstance(){ if(instance==null) //1 instance = new singleton(); //2 return instance } }
class singleton{ private singleton(){ //..... } private static singleton instance; public static singleton getinstance(){ if(instance==null) { //1 sycronized(singleton.class){ if(instance==null) instance = new singleton(); //2 } } return instance; } }
class singleton{ private singleton(){ //..... } private static singleton instance = new singleton(); public static singleton getinstance(){ return instance; } }
标签:
原文地址:http://www.cnblogs.com/jugg1024/p/4204965.html