单例设计模式
1、 什么是单例?
单例:在JVM执行的过程中,只能有一个实例。
2、 单例模式分为饿汉式和懒汉式
一、饿汉式:天生线程安全,因为饿汉式是在class被加载的时候创建的对象,所以不存在线程安全问题。由于懒汉式加了同步代码块,所以饿汉式的效率比懒汉式要高
二、懒汉式:我们需要创建对象的时候才创建对象,比较节省内存,由于加了同步代码块保证线程安全,所以效率很变低。
饿汉式单例的实现
class HungrySingleton{
//在class被JVM加载时创建单例对象,static是保证只有一个对象
private static final HungrySingleton singleton = new HungrySingleton();
/**
* 私有化构造器的目的是为了不让别人new对象
* 反射也不可以创建对象了
*/
private HungrySingleton() {
}
/**
* 创建一个获取单例对象的方法
*/
public static HungrySingleton getHungrySingleton(){
return singleton;
}
}
public class HungrySingletonTest {
public static void main(String[] args) {
HungrySingleton s1 = HungrySingleton.getHungrySingleton();
HungrySingleton s2 = HungrySingleton.getHungrySingleton();
System.out.println(s1);
System.out.println(s2);
System.out.println(s1==s2);
}
}
实现结果
如果不使用私有构造器
class HungrySingleton{
//在class被JVM加载时创建单例对象,static是保证只有一个对象
private static final HungrySingleton singleton = new HungrySingleton();
/**
* 创建一个获取单例对象的方法
*/
public static HungrySingleton getHungrySingleton(){
return singleton;
}
}
public class HungrySingletonTest {
public static void main(String[] args) {
HungrySingleton s1 = HungrySingleton.getHungrySingleton();
HungrySingleton s2 = HungrySingleton.getHungrySingleton();
HungrySingleton s3 = new HungrySingleton();
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
输出结果
懒汉式单例的实现
我没有加synchronized,向大家展示一下线程安全问题
/**
* 懒汉式单例
* @author 紫炎易霄
*/
class LazySingleton{
//加static保证只有一个实例
private static LazySingleton singleton;
/**
* 私有化构造器保证不能被人new
*/
private LazySingleton(){
}
/**
* 提供一个获得单例的方法
*/
public static LazySingleton getLazySingleton(){
if(singleton == null){
singleton = new LazySingleton();
}
return singleton;
}
}
class TreadLazySingletion implements Runnable{
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 5; i++) {
LazySingleton lazySingleton = LazySingleton.getLazySingleton();
System.out.println(lazySingleton);
}
}
}
public class LazySingletonTest {
public static void main(String[] args) {
TreadLazySingletion treadLazySingletion = new TreadLazySingletion();
Thread t1 = new Thread(treadLazySingletion);
Thread t2 = new Thread(treadLazySingletion);
t1.start();
t2.start();
}
}
运行结果
加锁之后(大家可以多试试运行结果,我运行了十几次没有发生线程安全问题)
/**
* 懒汉式单例
* @author 紫炎易霄
*/
class LazySingleton{
//加static保证只有一个实例
private static LazySingleton singleton;
/**
* 私有化构造器保证不能被人new
*/
private LazySingleton(){
}
/**
* 提供一个获得单例的方法
*/
public static LazySingleton getLazySingleton(){
synchronized (LazySingleton.class) {
if(singleton == null){
singleton = new LazySingleton();
}
}
return singleton;
}
}
class TreadLazySingletion implements Runnable{
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 50; i++) {
LazySingleton lazySingleton = LazySingleton.getLazySingleton();
System.out.println(lazySingleton);
}
}
}
public class LazySingletonTest {
public static void main(String[] args) {
TreadLazySingletion treadLazySingletion = new TreadLazySingletion();
Thread t1 = new Thread(treadLazySingletion);
Thread t2 = new Thread(treadLazySingletion);
t1.start();
t2.start();
}
}
运行结果