자바 단일 모드 다양한 구현

3951 단어
/**
* 
* @author cocacoca
*
*/
public class Main {
    public static void main(String[] args) {
        Instance_v1.instance.print();
        
        Instance_v2.instance.print();
        
        Instance_v3.getInstance().print();
        
        Instance_v4.getInstance().print();
        
        Instance_v5.getInstance().print();
        
        Instance_v6.instance.print();
        
        Instance_v7.getInstance().print();
    }
    
    public static class Instance_v1 {
        public static final Instance_v1 instance = new Instance_v1();
        
        public void print(){
            System.out.println("Instance_v1");
        }
    }
    
    public static class Instance_v2 {
        public static Instance_v2 instance = null;
        
        static {
            instance = new Instance_v2();
        }
        
        public void print(){
            System.out.println("Instance_v2");
        }
    }
    
    public static class Instance_v3 {
        private static Instance_v3 instance = null;
        
        public static synchronized Instance_v3 getInstance() {
            if (instance == null) {
                instance = new Instance_v3();
            }
            return instance;
        }
        
        public void print(){
            System.out.println("Instance_v3");
        }
    }
    
    public static class Instance_v4 {
        private volatile static Instance_v4 instance = null;
        
        public static Instance_v4 getInstance() {
            if (instance == null) {
                synchronized (Instance_v4.class) {
                    if (instance == null) {
                        instance = new Instance_v4();
                    }
                }
            }
            return instance;
        }
        
        public void print(){
            System.out.println("Instance_v4");
        }
    }
    
    public static class Instance_v5 {
        private static class SigletonHolder {
            private static final Instance_v5 instance = new Instance_v5();
        }
        
        public static synchronized Instance_v5 getInstance() {
            return SigletonHolder.instance;
        }
        
        public void print(){
            System.out.println("Instance_v5");
        }
    }
    
    public static enum Instance_v6 {
        instance;
        
        public void print(){
            System.out.println("Instance_v6");
        }
    }
    
    public static class Instance_v7 {
        private static Instance_v7 instance = null;

        public static Instance_v7 getInstance() {
            if (instance == null) {
                instance = new Instance_v7();
            }
            return instance;
        }

        public void print() {
            System.out.println("Instance_v7");
        }
    }
}

시간 소모 테스트
이루어지다
테스트 결과
Instance_v1
17,15,14,14,12,8,7,6,7,4
Instance_v2
17,14,14,11,11,8,9,6,5,5
Instance_v3
16,18,14,12,11,8,7,6,5,5
Instance_v4
16,15,13,11,15,10,7,6,4,5
Instance_v5
17,15,14,11,11,18,5,5,4,4
Instance_v6
22,16,15,14,15,11,8,8,5,7
Instance_v7
17,15,14,11,11,8,7,6,6,5
다 중 루틴 테스트
이루어지다
테스트 결과
결론.
Instance_v1
start.Main$Instance_v1@72093dcd start.Main$Instance_v1@72093dcd
안전 하 다.
Instance_v2
start.Main$Instance_v2@72093dcd start.Main$Instance_v2@72093dcd
안전 하 다.
Instance_v3
start.Main$Instance_v3@72093dcd start.Main$Instance_v3@72093dcd
안전 하 다.
Instance_v4
start.Main$Instance_v4@72093dcd start.Main$Instance_v4@72093dcd
안전 하 다.
Instance_v5
start.Main$Instance_v5@72093dcd start.Main$Instance_v5@72093dcd
안전 하 다.
Instance_v6
Instance_v7
start.Main$Instance_v7@35d9dc39 start.Main$Instance_v7@72093dcd
안전 하지 않다

좋은 웹페이지 즐겨찾기