zoukankan      html  css  js  c++  java
  • 使用静态代码块来实现单例模式

    package com.wz.thread.staticlump;
    /**
     * 使用静态代码块来实现单例模式
     * @author Administrator
     *
     */
    public class MyObject {
        
        private static MyObject instance = null;
        
        private MyObject() {}
        static {
            instance = new MyObject();
        }
        public static MyObject getInstance() {
            return instance;
        }

    }

    package com.wz.thread.staticlump;

    public class MyThread extends Thread {
        
        @Override
        public void run() {
            super.run();
            for (int i = 0; i < 5; i++) {
                System.out.println(MyObject.getInstance().hashCode());
            }
            
        }

    }

    package com.wz.thread.staticlump;
    /**
     * 输出的hascode值相同,说明是同一个对象
     * @author Administrator
     *
     */
    public class Run {

        public static void main(String[] args) {
            MyThread t1 = new MyThread();
            MyThread t2 = new MyThread();
            MyThread t3 = new MyThread();
            t1.start();
            t2.start();
            t3.start();
        }
    }

  • 相关阅读:
    LDAP+Jenkins
    LDAP+SASL+SVN
    Ubuntu下的LDAP服务搭建
    LDAP+Confluence
    LDAP+Nextcloud
    vim -d
    linux默认编辑器
    vim相关调优
    MySQL中的保留字
    RHEL7配置ip地址
  • 原文地址:https://www.cnblogs.com/wadmwz/p/7822340.html
Copyright © 2011-2022 走看看