• 线程安全的单例-懒汉


    # 普通的单例

    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;
        }
    }
  • 相关阅读:
    史上最简洁的handler原理解释
    handler解惑
    Http中get和post的区别
    使用软引用缓存Bitmap
    Request头和Response头
    DNS编程实验--域名与IP的相互转换
    CString与string
    C++ string占多少个字节测试
    java中类的继承性和多态性实例
    java寻找html文件中的标签
  • 原文地址:https://www.cnblogs.com/lwmp/p/11278374.html
Copyright © 2020-2023  润新知