题目:设计一个类,我们只能生成该类的一个实例

package com.cxz.demo02;

/**
 * Created by CXZ on 2016/9/13.
 */
public class SingletonTest {

    /**
     * 单例模式,饿汉式,线程安全
     */
    public static class Singleton {
        private final static Singleton INSTANCE = new Singleton();
        private Singleton() {

        }
        public static Singleton getInstance() {
            return INSTANCE;
        }
    }

    /**
     * 单例模式,懒汉式,线程不安全
     */
    public static class Singleton2 {
        private static Singleton2 instance = null;
        private Singleton2() {

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

    /**
     * 单例模式,懒汉式,线程安全,多线程环境下效率高
     * synchronized够保证在同一时刻最多只有一个线程执行该段代码
     */
    public static class Singleton3 {
        private static Singleton3 instance = null;
        private Singleton3() {

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

    /**
     * 单例模式,懒汉式,变种,线程安全
     */
    public static class Singleton4 {
        private static Singleton4 instance = null;
        static {
            instance = new Singleton4();
        }
        private Singleton4() {

        }
        public static Singleton4 getInstance() {
            return instance;
        }
    }

    /**
     * 单例模式,使用静态内部类,线程安全
     */
    public static class Singleton5 {
        private final static class SingletonHolder {
            private static final Singleton5 INSTANCE = new Singleton5();
        }
        private Singleton5() {

        }
        public static Singleton5 getInstance() {
            return SingletonHolder.INSTANCE;
        }
    }

    /**
     * 静态内部类,使用枚举方式,线程安全
     */
    public enum Singleton6 {
        INSTANCE;
        public void whateverMethon() {

        }
    }

    /**
     * 静态内部类,使用双重校验,线程安全
     */
    public static class Singleton7 {
        private volatile static Singleton7 instance = null;
        private Singleton7() {

        }
    }

    /**
     * main
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(Singleton.getInstance() == Singleton.getInstance());
        System.out.println(Singleton2.getInstance() == Singleton2.getInstance());
        System.out.println(Singleton3.getInstance() == Singleton3.getInstance());
        System.out.println(Singleton4.getInstance() == Singleton4.getInstance());
        System.out.println(Singleton5.getInstance() == Singleton5.getInstance());
        System.out.println(Singleton6.INSTANCE == Singleton6.INSTANCE);
    }

}

剑指Offer-【面试题02:实现Singleton 模式——七种实现方式】的更多相关文章

  1. 剑指Offer:面试题15——链表中倒数第k个结点(java实现)

    问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...

  2. 剑指offer面试题3 二维数组中的查找(c)

    剑指offer面试题三:

  3. 剑指Offer——笔试题+知识点总结

    剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...

  4. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  5. C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解

    剑指offer  面试题23:从上往下打印二叉树 参与人数:4853  时间限制:1秒  空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...

  6. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  7. Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)

    剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...

  8. C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解

    剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...

  9. C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解

    剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...

随机推荐

  1. Table 固定表头的几种方法

    <style type="text/css"> /*所有内容都在这个DIV内*/ div.all { border: 3px solid #FF00FF; width: ...

  2. redis 操作string 的测试

    1>set  set name zhangsan :OK get name: zhangsan set name lisi:OK get name: lisi   2> setnx 如果存 ...

  3. centos 6.4 getmail 收取163 邮件

    #CentOS 6.6 64bit 默认yum 源没有getmail rpm包#首先安装EPEL yum 源EPEL(Extra Packages for Enterprise Linux):http ...

  4. 移动端Web开发调试之Chrome远程调试(Remote Debugging)

    比如手机钉钉调试页面,下面是一位同学整理的链接: http://blog.csdn.net/freshlover/article/details/42528643/ 如果inspect 后,一直空白, ...

  5. redis中的跳跃表

    参考:http://www.leoox.com/?p=347

  6. Nacl开发

    环境搭建:http://www.bojinxiaozhu.com/2014/0221/80.html http://blog.csdn.net/xoyojank/article/details/814 ...

  7. python2.7 学习笔记--列表的使用

    同其它编程语言一样,python也提供了丰富的数据结构,以方便数据的处理.本文介绍两种最基本的数据集合,列表和元组的使用. 一.列表使用介绍 可以理解为一个有序的序列.其使用方式举例如下: list= ...

  8. c#在字符串中计算加减乘除...

                DataTable dt = new DataTable();             Response.Write(dt.Compute("1+1*5", ...

  9. tab 切换写法

    <script>        var oUL = document.getElementById('aboutTab-ul');        var oLi = oUL.getElem ...

  10. 整理文件,翻出了以前作的ps稿 (^o^)c旦``

    稍稍会那么一点PS,小意思