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

      1 public class Demo2 {
      2     public static void main(String[] args) {
      3         /*
      4          * 懒汉式单线程环境下
      5         SingleDemo instance1 = SingleDemo.getInstance();
      6         SingleDemo instance2 = SingleDemo.getInstance();
      7         System.out.println(instance1);
      8         System.out.println(instance2);
      9         System.out.println(instance1==instance2);//true
     10         */
     11         
     12         /**
     13          * 懒汉式多线程环境下
     14          */
     15         /*Runnable mythread = new MyThread();
     16         Thread t1 = new Thread(mythread);
     17         Thread t2 = new Thread(mythread);
     18         t1.start();
     19         t2.start();*/
     20         
     21         
     22         //饱汉式单例多线程环境下
     23         //由于饱汉式单例是在加载类时就已经创建好了对象,而非是在线程执行时创建对象,所以饱汉式单例本身就是线程安全的
     24         Runnable mythread2 = new MyThread2();
     25         Thread t1 = new Thread(mythread2);
     26         Thread t2 = new Thread(mythread2);
     27         t1.start();
     28         t2.start();
     29     }
     30     
     31 }
     32 
     33 /**
     34  * 用于测试懒汉式单例
     35  */
     36 class MyThread implements Runnable {
     37     @Override
     38     public void run() {
     39         System.out.println(SingleDemo.getInstance());
     40     }
     41 }
     42 
     43 /**
     44  * 用于测试饱汉式单例
     45  */
     46 class MyThread2 implements Runnable {
     47     @Override
     48     public void run() {
     49         System.out.println(SingleDemo2.getInstance());
     50     }
     51 }
     52 
     53 
     54 /**
     55  * 饿汉式单例
     56  * 1.构造器私有化
     57  * 2.声明一个私有的静态变量并初始化
     58  * 3.公开访问该变量的方法
     59  */
     60 class SingleDemo2{
     61     private static SingleDemo2 instance = new SingleDemo2();
     62     private SingleDemo2() {
     63         
     64     }
     65     
     66     public static SingleDemo2 getInstance() {
     67         return instance;
     68     }
     69     
     70 }
     71 
     72 
     73 /**
     74  * 使用静态内部类的方式
     75  * 这是饱汉式单例的一种变形,类只有在使用时才会被加载,只要类加载,那么其静态属性就会被初始化,与直接在类中声明属性的方式相比,在静态内部类
     76  * 中声明的属性只会在调用getInstance()时,才会初始化实例,提高了效率
     77  */
     78 class SingleDemo3{
     79     private SingleDemo3() {
     80         
     81     }
     82     
     83     private static class Innerclass {
     84         private static SingleDemo3 instance = new SingleDemo3();
     85 
     86     }
     87     
     88     public static SingleDemo3 getInstance() {
     89         return Innerclass.instance;
     90     }
     91 }
     92 
     93 
     94 /**
     95  * 懒汉式单例
     96  * 1.构造器私有化
     97  * 2.声明私有的静态变量
     98  * 3.公开访问该变量的方法
     99  * 
    100  * @author tele
    101  *
    102  */
    103 class SingleDemo {
    104     private static SingleDemo instance; 
    105     
    106     private SingleDemo() {
    107         
    108     }
    109     
    110     //线程不安全
    111     public static  SingleDemo getInstance1() {
    112         if(instance ==  null) {
    113             instance = new SingleDemo();
    114         }
    115         return instance;
    116     }
    117     
    118     //synchronized关键字
    119     public static synchronized SingleDemo getInstance2() {
    120         if(instance ==  null) {
    121             instance = new SingleDemo();
    122         }
    123         return instance;
    124     }
    125     
    126     //synchronized(类.class)
    127     public static SingleDemo getInstance3() {
    128         synchronized (SingleDemo.class) {
    129             if(instance ==  null) {
    130                 instance = new SingleDemo();
    131             }
    132             return instance;
    133         }
    134     }
    135     
    136     //双重检查以提高效率
    137     public static SingleDemo getInstance() {
    138         if (instance == null) {
    139             synchronized (SingleDemo.class) {
    140                 if(instance ==  null) {
    141                     instance = new SingleDemo();
    142                 }
    143             }
    144         }
    145         return instance;
    146     }
    147 }

    Runtime类中就使用了饱汉式单例模式

  • 相关阅读:
    .NET正则基础之——平衡组
    网站架构探索负载均衡的方式
    Sql2005 全文索引详解
    构架师之路(4) 里氏代换原则(Liskov Substitution Principle, LSP)
    Ubuntu 9.04 server安装nginx+php(fastcgi)
    构架师之路(2) 开闭原则(OpenClosed Principle,OCP)
    SQL Server中常用全局变量介绍
    构架师之路(3) 1 IoC理论的背景
    linux ubuntu 命令集合
    理解nginx 和 php(fastcgi)的关系
  • 原文地址:https://www.cnblogs.com/tele-share/p/9475344.html
Copyright © 2011-2022 走看看