zoukankan      html  css  js  c++  java
  • 线程安全的单例模式

    一饿汉模式

    package com.thread;
    
    public class Test8 {
        public static void main(String[] args) {
            MyThread t1 = new MyThread();
            MyThread t2 = new MyThread();
            MyThread t3 = new MyThread();
            
            t1.start();
            t2.start();
            t3.start();
            
        }
    }
    
    class MyObject{
        private static MyObject myObject = new MyObject();
        
         public static MyObject getInstance(){
            return myObject;
        }
    }
    
    class MyThread extends Thread{
        public void run(){
            System.out.println(MyObject.getInstance().hashCode());
        }
    }

    二、懒汉模式(线程安全)

    package com.thread;
    
    public class Test5 {
        public static void main(String[] args) {
            MyThread t1 = new MyThread();
            MyThread t2 = new MyThread();
            MyThread t3 = new MyThread();
            
            t1.start();
            t2.start();
            t3.start();
            
        }
    }
    
    class MyObject{
        private static MyObject myObject;
        
         public static MyObject getInstance(){
            
            if(myObject==null){
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 synchronized (MyObject.class) {
                     if(myObject == null){
                         myObject = new MyObject();    
                         System.out.println(Thread.currentThread().getName()+" 新建实例");
                     }
                    
                }
                
                
            }
            return myObject;
        }
    }
    
    class MyThread extends Thread{
        public void run(){
            System.out.println(MyObject.getInstance().hashCode());
        }
    }

    三、使用静态内置类实现单例模式

    package com.thread;
    
    public class Test6 {
        public static void main(String[] args) {
            MyThread t1 = new MyThread();
            MyThread t2 = new MyThread();
            MyThread t3 = new MyThread();
            
            t1.start();
            t2.start();
            t3.start();
            
        }
    
    }
    
    class MyObject{
        private static final long serivalizableUID = 888l;
        
        private static class MyobjectHandle{
            private static final MyObject myObject= new MyObject();
        }
        
        public static MyObject getInstance(){
            return MyobjectHandle.myObject;
        }
        
        
        
    }
    
    class MyThread extends Thread{
        public void run(){
            System.out.println(MyObject.getInstance().hashCode());
        }
    }

    四、使用static代码块实现单例模式

    package com.thread;
    
    public class Test7 {
        public static void main(String[] args) {
            MyThread t1 = new MyThread();
            MyThread t2 = new MyThread();
            MyThread t3 = new MyThread();
            
            t1.start();
            t2.start();
            t3.start();
            
        }
    
    }
    
    class MyObject{
        
        private static MyObject instance = null;
        
        static{
            instance = new MyObject();
        }
        
        public static MyObject getInstance(){
            return instance;
        }
        
    }
    
    class MyThread extends Thread{
        public void run(){
            System.out.println(MyObject.getInstance().hashCode());
        }
    }

     五、枚举实现单例模式

      待完善

  • 相关阅读:
    IIS网站应用偶尔出现"服务不可用"或者显示乱码字体
    mac os下切换pip3国内源并安装requests库
    mysql5.6运行一段时间之后网站页面出现乱码解决办法
    mac pro下安装安装 SymPy 和 matplotlib报错解决方案
    python3汉诺塔简单实现代码
    用python提取xml里面的链接源码
    Mac环境下 jieba 配置记录
    AngularJS 整理学习
    Java有关List的stream基本操作
    Callable的简单使用
  • 原文地址:https://www.cnblogs.com/ouyy/p/9025506.html
Copyright © 2011-2022 走看看