标签:多线程 静态 sig ret instance tac == class interrupt
还是套路问题,一种思想而已,两种方式
1 dubble check instance
2 static inner class
两次检测加类锁
静态内部类,其实就是饿汉模式,直接给你就好了
package Concurrency; class dubbleCheck{ private static dubbleCheck ds ; public dubbleCheck getInstance(){ if(ds == null){ try{ Thread.sleep(50); synchronized (dubbleCheck.class){ if(ds==null){ ds = new dubbleCheck(); } } }catch (InterruptedException e){ e.printStackTrace(); } } return ds; } protected dubbleCheck(){ } } class InnerSigleton { private static class Sigleton{ private static InnerSigleton is = new InnerSigleton(); } public static InnerSigleton getInstance(){ return Sigleton.is; } }
标签:多线程 静态 sig ret instance tac == class interrupt
原文地址:https://www.cnblogs.com/corx/p/10049864.html