• java多线程基本概述(十二)——多线程与单例


    1:饿汉模式:

    package soarhu;
    
    class Singleton {
    
       private Singleton(){}
    
       private static final  Singleton SINGLETON = new Singleton();
    
       public static  Singleton getInstance(){
           return Singleton.SINGLETON;
       }
    }
    public class Test{
        public static void main(String[] args) throws InterruptedException {
            for (int i = 0; i < 10; i++) {
                new Thread(){
                    @Override
                    public void run() {
                        Singleton singleton = Singleton.getInstance();
                        System.out.println(singleton);
                    }
                }.start();
            }
        }
    }

    输出结果:

    soarhu.Singleton@f3f9f4b
    soarhu.Singleton@f3f9f4b
    soarhu.Singleton@f3f9f4b
    soarhu.Singleton@f3f9f4b
    soarhu.Singleton@f3f9f4b
    soarhu.Singleton@f3f9f4b
    soarhu.Singleton@f3f9f4b
    soarhu.Singleton@f3f9f4b
    soarhu.Singleton@f3f9f4b
    soarhu.Singleton@f3f9f4b
    
    Process finished with exit code 0

    2:懒汉模式:

    package soarhu;
    
    class Singleton {
    
       private Singleton(){}
    
       private static volatile Singleton SINGLETON = null;
    
       public static  Singleton getInstance(){
           if (null==SINGLETON){
               synchronized (Singleton.class){
                   if(null==SINGLETON){
                       SINGLETON=new Singleton();
                   }
               }
           }
           return Singleton.SINGLETON;
       }
    }
    public class Test{
        public static void main(String[] args) throws InterruptedException {
            for (int i = 0; i < 10; i++) {
                new Thread(){
                    @Override
                    public void run() {
                        Singleton singleton = Singleton.getInstance();
                        System.out.println(singleton);
                    }
                }.start();
            }
        }
    }

    输出结果:

    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    
    Process finished with exit code 0

    3:静置内部类:

    package soarhu;
    
    class Singleton {
    
       private Singleton(){}
    
       private  static class Holder{
           private static  Singleton  SINGLETON= new Singleton();
       }
    
       public static  Singleton getInstance(){
           return Holder.SINGLETON;
       }
    }
    public class Test{
        public static void main(String[] args) throws InterruptedException {
            for (int i = 0; i < 10; i++) {
                new Thread(){
                    @Override
                    public void run() {
                        Singleton singleton = Singleton.getInstance();
                        System.out.println(singleton);
                    }
                }.start();
            }
        }
    }

    输出结果

    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    soarhu.Singleton@1a4ffe2
    
    Process finished with exit code 0

    4:如果要保证对象序列化后仍然保持单利,那么代码如下:

    package soarhu;
    
    import java.io.*;
    
    class Singleton implements Serializable{
    
        private static final long serialVersionUID = 123L;
    
        private Singleton(){}
    
        private  static class Holder{
            private static  Singleton  SINGLETON= new Singleton();
        }
    
        public static  Singleton getInstance(){
            return Holder.SINGLETON;
        }
    
        protected Object readResolve() throws ObjectStreamException{
            //diy serialize Strategy
            System.out.println("serialize");
            return getInstance();
        }
    
    
    }
    public class Test{
        public static void main(String[] args) throws Exception {
            Singleton singleton = Singleton.getInstance();
            FileOutputStream outputStream = new FileOutputStream(new File("d:\a.bat"));
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(singleton);
            objectOutputStream.close();
            outputStream.close();
            System.out.println("writing object "+singleton);
    
            FileInputStream fileInputStream = new FileInputStream(new File("d:\a.bat"));
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            Object o = objectInputStream.readObject();
            objectInputStream.close();
            fileInputStream.close();
            if (Singleton.class.isInstance(o)){
                Singleton s2 =Singleton.class.cast(o);
                System.out.println("reading object "+s2);
            }
        }
    }

    输出结果:

    writing object soarhu.Singleton@7f31245a
    serialize
    reading object soarhu.Singleton@7f31245a
    
    Process finished with exit code 0

    5:使用静态代码块实现单利

    package soarhu;
    
    class Singleton {
    
        private static  Singleton  SINGLETON = null;
    
        private Singleton(){}
    
       static {
            SINGLETON = new Singleton();
       }
    
        public static  Singleton getInstance(){
            return SINGLETON;
        }
    }
    public class Test{
        public static void main(String[] args) throws InterruptedException {
            for (int i = 0; i < 10; i++) {
                new Thread(){
                    @Override
                    public void run() {
                        Singleton singleton = Singleton.getInstance();
                        System.out.println(singleton);
                    }
                }.start();
            }
        }
    }

    输出结果:

    soarhu.Singleton@4763d146
    soarhu.Singleton@4763d146
    soarhu.Singleton@4763d146
    soarhu.Singleton@4763d146
    soarhu.Singleton@4763d146
    soarhu.Singleton@4763d146
    soarhu.Singleton@4763d146
    soarhu.Singleton@4763d146
    soarhu.Singleton@4763d146
    soarhu.Singleton@4763d146
    
    Process finished with exit code 0
  • 相关阅读:
    第三百九十九节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5安装mysql5.6
    第三百九十七节,Django+Xadmin打造上线标准的在线教育平台—其他插件使用说,主题本地化设置
    第三百九十八节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5系统环境设置
    第三百九十六节,Django+Xadmin打造上线标准的在线教育平台—其他插件使用说,自定义列表页上传插件
    第三百九十五节,Django+Xadmin打造上线标准的在线教育平台—Xadmin集成富文本框
    第三百九十四节,Django+Xadmin打造上线标准的在线教育平台—Xadmin后台进阶开发配置2,以及目录结构说明
    第三百九十三节,Django+Xadmin打造上线标准的在线教育平台—Xadmin后台进阶开发配置
    第三百九十二节,Django+Xadmin打造上线标准的在线教育平台—sql注入攻击,xss攻击,csrf攻击
    第三百九十一节,Django+Xadmin打造上线标准的在线教育平台—404,403,500页面配置
    第三百九十节,Django+Xadmin打造上线标准的在线教育平台—Django+cropper插件头像裁剪上传
  • 原文地址:https://www.cnblogs.com/soar-hu/p/6733300.html
Copyright © 2020-2023  润新知