懒汉模式,饿汉模式
懒汉模式:调用方法是才new对象
线程安全问题,加上synchronized修饰
- Public class Singleton{
- Private static Singleton instance;
- Private Singleton(){};
- Public static synchronized Singleton getinstance(){
- If(null==instance){
- Instance = new Singleton():
- }
- Return instance;
- }
- }
饿汉模式:直接new对象
这种方式基于classloder机制避免了多线程的同步问题
- Public class Singleton{
- Private static Singleton instance = new Singleton();
- Private Singleton();
- Public static Singleton getinstance(){
- Return instance;
- }
- }

浙公网安备 33010602011771号