zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Non Clustered Confluence: Database is being updated by another Confluence instance
    晦涩时光 浮光掠影
    迷雾重重 星光闪耀
    navicat 查询窗口快捷键
    sublime使用技巧记录
    mvn 手动打包并放置到本地仓库下
    git bash 风格调整(显示中文)
    windows控制台(console)乱码
    java调用第三方命令,process.waitfor()挂起(你不知道的坑)
    shell问题-报错即退出
  • 原文地址:https://www.cnblogs.com/soar-hu/p/6733300.html
Copyright © 2011-2022 走看看