package com.demo01;

public class Single {

	/**
* 设计模式:单例设计模式
* 解决一个类中只允许存在一个对象这种情况;
* 不允许通过类,无限制的创建该类的对象。
* 怎样实现单例模式?
* 构造函数私有化(private);
* 在当前类的内部,写一个方法创建出该类的对象;
* 对外提供公共的访问接口;
* */
public static void main(String[] args) { } }
/*
* 枚举enum实现单例模式
* 多线程下安全
*/
class ClassFactory{
private enum MyEnumSingleton{
singletonFactory; private MSingleton instance; private MyEnumSingleton(){//枚举类的构造方法在类加载是被实例化
instance = new MSingleton();
} public MSingleton getInstance(){
return instance;
}
}
public static MSingleton getInstance(){
return MyEnumSingleton.singletonFactory.getInstance();
}
}
//需要获实现单例的类,比如数据库连接Connection
class MSingleton{
public MSingleton(){}
} /*
* 内部类方式实现单例
* 多线程是安全
*/ class MySingleton { //静态内部类
private static class MySingletonHandler{
private static MySingleton instance = new MySingleton();
}
private MySingleton(){}
public static MySingleton getInstance() {
return MySingletonHandler.instance;
}
} /*
* 懒汉式:
* 在需要用的时候才创建对象
* 多线程下微调后,多线程下,无法保证实例唯一的,
*/
class Singer2{
private Singer2(){}
private static Singer2 createSinger = null;
//创建单例,
public static Singer2 getInstance(){
if(createSinger == null){
try {
Thread.sleep(200);
createSinger = new Singer2();
} catch (InterruptedException e) {
e.printStackTrace();
} }
return createSinger;
} }
/*
* 懒汉式 方法加锁实现线程同步
* 加锁后效率低
*/ class Singer3{
private Singer3(){}
private static Singer3 createSinger = null;
//创建单例,synchronized加锁
public static synchronized Singer3 getInstance(){
if(createSinger == null){
try {
Thread.sleep(200);
createSinger = new Singer3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return createSinger;
} }
/*
* 懒汉式 同步代码块实现
* 效率低
*/
class Singer5{
private Singer5(){}
private static Singer5 createSinger = null;
//创建单例,synchronized加锁
public static Singer5 getInstance(){
try {
synchronized (Singer5.class) {
if(createSinger == null){
Thread.sleep(200);
createSinger = new Singer5();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return createSinger;
} }
/*
* 懒汉式:
* 双检查锁机制DCL(double check lock)
* 销量和同步并存
*/
class Singer1{
private Singer1(){}
private static Singer1 createSinger = null;
//创建单例,synchronized加锁
public static Singer1 getInstance(){
try {
if(createSinger == null){
Thread.sleep(200);
synchronized (Singer1.class) {
if (createSinger==null) {
createSinger = new Singer1();
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return createSinger;
} } /*
* 俄汉式
* 开始就产生了对象。对于项目中从开始到结束都没有用到的对象,会造成资源浪费
* 多线程下测试安全,是同一个实例
*/
class Singer4{
private static Singer4 instance = new Singer4();
private Singer4(){}
public static Singer4 getInstance(){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
return instance;
}
}

多线程下的测试代码

package com.demo01;

public class MyThread extends Thread{

    public void run() {
System.out.println(ClassFactory.getInstance().hashCode());
} public static void main(String[] args) { MyThread[] mts = new MyThread[10];
for(int i = 0 ; i < mts.length ; i++){
mts[i] = new MyThread();
} for (int j = 0; j < mts.length; j++) {
mts[j].start();
}
}
}

  

05-11 03:22