zoukankan      html  css  js  c++  java
  • 单例模式

          设计模式大家都很熟悉,今天来记录下最基础也最简单的单例模式

          单例模式分为两类,一是饿汉式,另外就是相对的懒汉式

          想来看看饿汉式

          常见的实现方式如下:

     1 public class SingletonHungry {
     2     private static SingletonHungry instance = new SingletonHungry();
     3 
     4     private String token;
     5 
     6     private SingletonHungry() {
     7         token = System.currentTimeMillis() + "";
     8     }
     9 
    10     public static SingletonHungry getInstance() {
    11         return instance;
    12     }
    13 
    14     public String getToken() {
    15         return token;
    16     }
    17 }

    新增token来验证线程安全

    来个线程安全的

    public class SingletonHungrySafe {
    
        private static String token;
    
        //jvm加载时会实例化
        private static class Holder {
            private static SingletonHungrySafe INSTANCE = new SingletonHungrySafe();
        }
    
        private SingletonHungrySafe() {
            token = System.currentTimeMillis() + "";
        }
    
        public static final SingletonHungrySafe getInstance() {
            return Holder.INSTANCE;
        }
    
        public String getToken() {
            return token;
        }
    }
    可以保证在各个线程获取Holder.INSTANCE变量之前完成。在保证线程安全的同时,又可以延迟实例化,并且没有降低并发性

    接下来看看懒汉式,相对的,懒汉式是用时才加载,延后性。
    先来个线程不安全的
    public class SingletonLazy {
        private static SingletonLazy instance;
    
        private SingletonLazy() {
        }
    
        public static SingletonLazy getInstance() {
            if (instance == null) {
                instance = new SingletonLazy();
            }
            return instance;
        }
    }

    线程安全的

    public class SingletonLazySafe {
        private volatile static SingletonLazySafe instance;
    
        private SingletonLazySafe() {
        }
    
        public static SingletonLazySafe getInstance() {
            if (null == instance) {
                synchronized (SingletonLazySafe.class) {
                    if (null == instance) {
                        instance = new SingletonLazySafe();
                    }
                }
            }
            return instance;
        }
    }
    双重检查加锁,保证线程安全。

    同时,我们可以对这些进行测试
    public class SingletonTest {
    
        public static void main(String[] args) throws InterruptedException {
            for (int i = 0; i < 1000; i++) {
                Single single = new Single();
                single.start();
            }
        }
    
        static class Single extends Thread {
    
            @Override
            public void run() {
                checkSingleHungry();
            }
        }
    
        private static void checkSingleHungrySafe() {
            SingletonHungrySafe singletonHungrySafe = SingletonHungrySafe.getInstance();
            System.out.println("Thread id=" + Thread.currentThread().getId() + "token:" + singletonHungrySafe.getToken());
        }
    
        private static void checkSingleHungry() {
            SingletonHungry singletonHungry = SingletonHungry.getInstance();
            System.out.println("Thread id=" + Thread.currentThread().getId() + "token:" + singletonHungry.getToken());
        }
    }

    验证线程安全问题

          

  • 相关阅读:
    循环计时器
    在一个表格里,超过一定宽度字符串进行截取显示点点,鼠标移上去显示全
    判断单选框是否被选中
    美化的select下拉框
    js获取网页高度
    Bootstrap的使用。。。
    解决网站出现GET .woff 404 (Not Found)的问题
    Bootstrap 字体图标(Glyphicons)
    一个设置为display:none;的div,在用.height()方法获取不到它的高,获取到的高度为0.
    substring() slice() substr()的区别联系
  • 原文地址:https://www.cnblogs.com/xzshare/p/12239942.html
Copyright © 2011-2022 走看看