单例模式的线程安全

package com.day3;

public class SingleTonDemo {
   public static void main(String[] args) {
       SinleTonThread sinleTonThread=new SinleTonThread();
       Thread thread=new Thread(sinleTonThread);
       thread.start();
       Thread thread2=new Thread(sinleTonThread);
       thread2.start();
}
}
class SinleTonThread  implements Runnable{

    @Override
    public void run() {
        SingleTon.getinstance();
        
    }
    
}

class SingleTon{
    private static SingleTon singleTon=null;
    private SingleTon() {
        System.out.println("单例模式");
    }
    public static SingleTon getinstance(){
        if(singleTon==null) {
            synchronized (SingleTon.class) {
                if(singleTon==null) {
                    singleTon=new SingleTon();
                }
            }    
        }
        
        
        return singleTon;
        
    }
}

 

posted @ 2018-11-27 00:09  言西早石头侠  阅读(122)  评论(0编辑  收藏  举报