zoukankan      html  css  js  c++  java
  • 线程安全的单例-懒汉

    # 普通的单例

    public class QQ {
        public static void main(String[] args) throws InterruptedException {
    
            final CountDownLatch latch = new CountDownLatch(10);
    
            for (int i = 0; i < 10; i++) {
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        MyClient.getInstance();
                        latch.countDown();
                    }
    
                }.start();
            }
    
            latch.await();
    
            System.out.println(MyClient.count);
        }
    }
    
    class MyClient {
    
        private static MyClient client;
    
        public static int count;
    
        private MyClient() {
            count++;
        }
    
        public static MyClient getInstance() {
            if (client == null) {
                client = new MyClient();
            }
            return client;
        }
    }

    # 普通的线程安全的单例

    public class QQ {
        public static void main(String[] args) throws InterruptedException {
    
            final CountDownLatch latch = new CountDownLatch(10);
    
            for (int i = 0; i < 10; i++) {
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        MyClient.getInstance();
                        latch.countDown();
                    }
    
                }.start();
            }
    
            latch.await();
    
            System.out.println(MyClient.count);
        }
    }
    
    class MyClient {
    
        private static MyClient client;
    
        public static int count;
    
        private MyClient() {
            count++;
        }
    
        public synchronized static MyClient getInstance() {
            if (client == null) {
                client = new MyClient();
            }
            return client;
        }
    }

    # 双重校验的线程安全的单例

    public class QQ {
        public static void main(String[] args) throws InterruptedException {
    
            final CountDownLatch latch = new CountDownLatch(10);
    
            for (int i = 0; i < 10; i++) {
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        MyClient.getInstance();
                        latch.countDown();
                    }
    
                }.start();
            }
    
            latch.await();
    
            System.out.println(MyClient.count);
        }
    }
    
    class MyClient {
    
        private static MyClient client;
    
        public static int count;
    
        private MyClient() {
            count++;
        }
    
        public static MyClient getInstance() {
            if (client == null) {
                getSynInstance();
            }
            return client;
        }
    
        public synchronized static MyClient getSynInstance() {
            if (client == null) {
                client = new MyClient();
            }
            return client;
        }
    }
  • 相关阅读:
    Laravel自定义分页样式
    mysql中 key 、primary key 、unique key 和 index 有什么不同
    PHP RSA公私钥的理解和示例说明
    PHP操作Excel – PHPExcel 基本用法
    Yii 1.1 常规框架部署和配置
    阿里云服务器 Ubuntu 安装 LNMP
    全国地区sql表
    十道海量数据处理面试题与十个方法大总结
    Hibernate中对象的三种状态以及Session类中saveOrUpdate方法与merge方法的区别
    乐观锁与悲观锁——解决并发问题
  • 原文地址:https://www.cnblogs.com/lwmp/p/11278374.html
Copyright © 2011-2022 走看看