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;
        }
    }
  • 相关阅读:
    Cards CodeForces 399C
    CodeForces 25D Roads not only in Berland
    【转】别人家的八数码 A* IDA*解法
    Maze Stretching Poj3897 二分+BFS
    Data Handler 大模拟 + 双端链表 hdu 4268
    黑马程序员---java基础语法--流程控制与函数
    黑马程序员--java基础--其他对象
    黑马程序员------java基础----集合
    黑马程序员---java基础-----多态、内部类、异常、包
    黑马程序员--java基础------继承,抽象,接口
  • 原文地址:https://www.cnblogs.com/lwmp/p/11278374.html
Copyright © 2011-2022 走看看