zoukankan      html  css  js  c++  java
  • 设计模式 之 单例模式

    单例主要有两种,一种懒汉,一种饿汉,区别在于懒汉在getInstance的时候,才会去初始化单例,而饿汉在类加载之后就立即进行初始化。可见懒汉节省资源,而饿汉的在于天生的线程安全

    1.懒汉式单例

    package com.test.pattern;
    
    class Singleton {
        private static Singleton singleton;
        private Singleton(){
            System.out.println("hello I'm singleton "+this.hashCode());
        }
        public static Singleton getSingleton() {
            if(singleton == null)
                singleton = new Singleton();
            return singleton;
        }
    }
    public class SingletonTest {
        public static void main(String[] args) {
            Singleton.getSingleton();
            Singleton.getSingleton();
            Singleton.getSingleton();
        }
    }

    这种单例模式是线程不安全的(但如果将getSingleton方法声明为synchronized即可使之线程安全):

    package com.test.pattern;
    
    /**
     * @author wangx
     * @Date: 2016年8月8日 
     * @func: 懒汉式单例多线程不安全
     * @Copyright: 2016 wangx. All rights reserved.
     */
    public class SingletonMultithread {
        public static void main(String[] args) {
            new Thread(new Task()).start();
            new Thread(new Task()).start();
        }
    }
    
    class Task implements Runnable {
    
        @Override
        public void run() {
            Singleton.getSingleton();
            Singleton.getSingleton();
        }
    }

    运行结果:

    hello I'm singleton 1025866005
    hello I'm singleton 1449742585

    2.饿汉式单例

    package com.test.pattern;
    
    /**
     * @author wangx
     * @Date: 2016年8月9日 
     * @func: 饿汉式单例
     * @Copyright: 2016 wangx. All rights reserved.
     */
    class Singleton2 {
        private static Singleton2 singleton=new Singleton2();
        private Singleton2() {
            System.out.println("I'm singleton "+this.hashCode());
        }
        public static Singleton2 getInstance() {
            return singleton;
        }
    }
    public class SingletonTest2 {
        public static void main(String[] args) {
            new Thread(new Task2()).start();
            new Thread(new Task2()).start();
        }
    }
    
    class Task2 implements Runnable{
        @Override
        public void run() {
            Singleton2.getInstance();
        }
    }

    终:通过反射机制破坏单例

    package com.test.reflection;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    class HelloWorld {
        public void sayHello() {
            System.out.println("hello world");
        }
    }
    class Singleton {
        private Singleton(){}
        private static Singleton singleton;
        public Singleton getSingleton() {
            if(singleton == null)
                singleton = new Singleton();
            return singleton;
        }
        public void sayHello() {
            System.out.println("hello I'm singleton "+this.hashCode());
        }
    }
    public class ReflectionTest {
        public static void main(String[] args) throws NoSuchMethodException, SecurityException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
            Constructor constructor = Class.forName("com.test.reflection.Singleton").getDeclaredConstructor();
            constructor.setAccessible(true);
            Method sayHello = Class.forName("com.test.reflection.Singleton").getDeclaredMethod("sayHello", null);
            sayHello.invoke(constructor.newInstance(),null);
            sayHello.invoke(constructor.newInstance(),null);
        }
    }
  • 相关阅读:
    数据分析师入门——用 Pandas 进行数据预处理:数据清洗与可视化
    hdu 1532 Dinic模板(小白书)
    二分图的最大匹配、完美匹配和匈牙利算法(转)
    HDU 1532 (Dinic算法)
    HDU 1532 Drainage Ditches EK算法 flod算法
    Edmonds_Karp 算法入门详解(转)
    UVa 10801
    Codeforces Round #359 (Div. 2)C
    Codeforces Round #358 (Div. 2)B. Alyona and Mex
    int long long范围
  • 原文地址:https://www.cnblogs.com/heben/p/5749051.html
Copyright © 2011-2022 走看看