Java面试题:Singleton单例设计模式
1、认识Singleton
- Singleton:在Java中我们称之为单例设计模式
单例设计模式:即某个类在整个系统中只能有一个实例对象可以被获取和使用的代码模式
单:唯一
例:实例
2、要点
- 一是某个类只能有一个实例
构造器私有化
- 二是它必须自行创建这个实例
含有一个该类的静态变量来保存这个唯一的实例
- 三是它必须自行向整个系统提供这个实例
对外提供获取该实例对象的方式
(1) 直接暴露 (2) 用静态变量的get方法获取
3、常见形式
饿汉式:直接创建对象,不存在线程安全问题
- 直接实例化饿汉式 (简洁直观)
- 枚举式 (最简洁)
- 静态代码块饿汉式 (适合复杂实例化)
懒汉式:延迟创建对象
- 线程不安全 (适合单线程)
- 线程安全 (适合多线程)
- 静态内部类形式 (适合多线程)
3.1 直接实例化饿汉式
/*饿汉式:
* 在类初始化时直接创建对象,不管你是否需要这个对象都会创建
*
* 1、构造器私有化
* 2、自行创建,并且用静态变量保存
* 3、向外提供这个实例
* 4、强调这是一个实例,我们可以final修改
* */
public class Singleton1 {
public static final Singleton1 INSTANCE = new Singleton1();
private Singleton1(){
}
}
public class TestSingleton1 {
public static void main(String[] args) {
Singleton1 s = Singleton1.INSTANCE;
System.out.println(s);
}
}
3.2 枚举式
public enum Singleton2 {
INSTANCE
}
public class TestSingleton2 {
public static void main(String[] args) {
Singleton1 s = Singleton1.INSTANCE;
System.out.println(s);
}
}
3.3 静态代码块饿汉式
public class Singleton3 {
public static final Singleton3 INSTANCE;
private String info;
static{
try {
Properties pro = new Properties();
pro.load(Singleton3.class.getClassLoader().getResourceAsStream("single.properties"));
INSTANCE = new Singleton3(pro.getProperty("info"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Singleton3(String info){
this.info = info;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Singleton3 [info=" + info + "]";
}
}
public class TestSingleton3 {
public static void main(String[] args) {
Singleton3 s = Singleton3.INSTANCE;
System.out.println(s);
}
}
3.4 线程不安全
/*
* 懒汉式:
* 延迟创建这个实例对象
*
* (1)构造器私有化
* (2)用一个静态变量保存这个唯一的实例
* (3)提供一个静态方法,获取这个实例对象
* */
public class Singleton4 {
static Singleton4 instance;
private Singleton4(){
}
public static Singleton4 getInstance(){
if(instance==null){
instance = new Singleton4();
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
instance = new Singleton4();
}
return instance;
}
}
public class TestSingleton4 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
Callable<Singleton4> c = new Callable<Singleton4>(){
@Override
public Singleton4 call() throws Exception {
return Singleton4.getInstance();
}
};
ExecutorService es = Executors.newFixedThreadPool(2);
Future<Singleton4> f1 = es.submit(c);
Future<Singleton4> f2 = es.submit(c);
Singleton4 s1 = f1.get();
Singleton4 s2 = f2.get();
System.out.println(s1 == s2);
System.out.println(s1);
System.out.println(s2);
es.shutdown();
}
}
3.5 线程安全
/*
* 懒汉式:
* 延迟创建这个实例对象
*
* (1)构造器私有化
* (2)用一个静态变量保存这个唯一的实例
* (3)提供一个静态方法,获取这个实例对象
* */
public class Singleton5 {
static Singleton5 instance;
private Singleton5(){
}
public static Singleton5 getInstance(){
synchronized(Singleton5.class){
if(instance==null){
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
instance = new Singleton5();
}
}
return instance;
}
}
public class TestSingleton5 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
Callable<Singleton4> c = new Callable<Singleton4>(){
@Override
public Singleton4 call() throws Exception {
return Singleton4.getInstance();
}
};
ExecutorService es = Executors.newFixedThreadPool(2);
Future<Singleton4> f1 = es.submit(c);
Future<Singleton4> f2 = es.submit(c);
Singleton4 s1 = f1.get();
Singleton4 s2 = f2.get();
System.out.println(s1 == s2);
System.out.println(s1);
System.out.println(s2);
es.shutdown();
}
}
3.6 静态内部类形式
/*
* 在内部类被加载和初始化时,才创建INSTANCE实例对象
* 静态内部类不会自动随着外部类的加载和初始化而初始化,它是要单独去加载和初始化的
* 应为是在内部类加载和初始化时创建的,因此是线程是安全的
* */
public class Singleton6 {
private Singleton6(){
}
private static class Inner{
private static final Singleton6 INSTANCE = new Singleton6();
}
public static Singleton6 getInstance(){
return Inner.INSTANCE;
}
}
4、总结
如果是饿汉式,枚举类形式最简单
如果是懒汉式,静态内部类形式最简单