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
  • 相关阅读:
    关于组建“智彩足球技术研究团队”的说明
    2次成功投诉EMS和中国移动的经验
    为什么选择玩足球彩票以及玩彩票的心态?
    【原创】机器学习之PageRank算法应用与C#实现(1)算法介绍
    【原创】开源Math.NET基础数学类库使用(17)C#计算矩阵条件数
    【原创】开源Math.NET基础数学类库使用(16)C#计算矩阵秩
    【文章】本站收集与转载文章目录
    【原创】.NET读写Excel工具Spire.Xls使用(3)单元格控制
    分享一个Visual Studio的背景插件,让堆码更富情趣
    【原创】开源Math.NET基础数学类库使用(15)C#计算矩阵行列式
  • 原文地址:https://www.cnblogs.com/soar-hu/p/6733300.html
Copyright © 2011-2022 走看看