首页 > 其他 > 详细

单例模式

时间:2019-09-01 21:09:37      阅读:91      评论:0      收藏:0      [点我收藏+]

1、单例模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点。

2、代码示例:

2.1、饿汉模式:

技术分享图片
package com.aibaiyang.idemo.service.singleton;

/**
 * @Author zhong guo
 * @Date 2019/8/31 15:01
 * @description 饿汉单例模式(多线程环境依然是单例)
 **/
public class HungarySingleton {

    private static HungarySingleton instance = new HungarySingleton();

    private HungarySingleton(){

    }

    public static HungarySingleton getInstance(){
        return instance;
    }

}
View Code

2.2、懒汉模式:

技术分享图片
package com.aibaiyang.idemo.service.singleton;

/**
 * @Author zhong guo
 * @Date 2019/8/31 11:43
 * @description 懒汉单例模式
 **/
public class Singleton {

    private volatile static Singleton instance;

    /**
     * 构造方法让其private,这就堵死了外面利用new关键字
     * 创建此类示例的可能
     */
    private Singleton(){

    }

    /**
     * 此方法为获得此类示例的唯一访问点
     * @return
     */
    public static Singleton getInstance(){

        if(instance == null){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (Singleton.class){
                if(instance == null){
                    instance = new Singleton();
                }
            }
        }
        return instance;

    }

}
View Code

2.3、多线程类

HungaryThread类
技术分享图片
package com.aibaiyang.idemo.service.singleton;

/**
 * @Author zhong guo
 * @Date 2019/8/31 15:13
 * @description 饿汉线程
 **/
public class HungaryThread extends Thread{

    public void run(){
        //打印当前对象的hash值,相同则代表同一个对象,不同代表不同对象。
        System.out.println("当前对象hashCode:" + HungarySingleton.getInstance().hashCode());

    }

}
View Code
LazyThread类
技术分享图片
package com.aibaiyang.idemo.service.singleton;

/**
 * @Author zhong guo
 * @Date 2019/8/31 11:55
 * @description 懒汉线程
 **/
public class LazyThread implements Runnable{

    @Override
    public void run() {
        //打印当前对象的hash值,相同则代表同一个对象,不同代表不同对象。
        System.out.println("当前对象hashCode:" + Singleton.getInstance().hashCode());
    }

}
View Code

2.3、单例模式客户端:

SingletonClient类
技术分享图片
package com.aibaiyang.idemo.service.singleton;

/**
 * @Author zhong guo
 * @Date 2019/8/31 11:50
 * @description 单例模式客户端
 **/
public class SingletonClient {

    public static void main(String[] args){

        //1、懒汉单例实例
        lazySingletonDemo();

        //2、饿汉单例实例
        hungarySingletonDemo();


    }

    static void lazySingletonDemo(){

        //实例1
        Singleton singleton1 = Singleton.getInstance();

        //实例2
        Singleton singleton2 = Singleton.getInstance();

        if(singleton1 == singleton2){
            System.out.println("懒汉相同的实例!");
        }else {
            System.out.println("懒汉不同的实例");
        }

    }

    static void hungarySingletonDemo(){

        HungarySingleton hungarySingleton1 = HungarySingleton.getInstance();

        HungarySingleton hungarySingleton2 = HungarySingleton.getInstance();

        if(hungarySingleton1 == hungarySingleton2){
            System.out.println("饿汉相同的实例!");
        }else {
            System.out.println("饿汉不同的实例!");
        }

    }

}
View Code
SingletonTheadClient类
技术分享图片
package com.aibaiyang.idemo.service.singleton;

/**
 * @Author zhong guo
 * @Date 2019/8/31 12:04
 * @description 单例模式多线程客户端
 **/
public class SingletonTheadClient {

    public static void main(String[] args) {

        //1、懒汉多线程单例测试
        lazySingletonTest();

        //2、饿汉多线程单例测试
        hungarySingletonTest();

    }

    static void lazySingletonTest(){

        LazyThread myThread = new LazyThread();

        //线程1
        Thread thread1 = new Thread(myThread);

        //线程2
        Thread thread2 = new Thread(myThread);

        thread1.start();
        thread2.start();

    }

    static void hungarySingletonTest(){

        //线程1
        HungaryThread hungaryThread1 = new HungaryThread();

        //线程2
        HungaryThread hungaryThread2 = new HungaryThread();

        hungaryThread1.start();
        hungaryThread2.start();

    }

}
View Code

 

3、github:

单例模式

原文:https://www.cnblogs.com/aibaiyang/p/11443162.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!