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();
        }
    }

  • 相关阅读:
    4 决策树
    Seaborn中几种作图方式的比较
    centso7设置防火墙
    让普通用户拥有
    TensorFlow 训练只用cpu
    loss训练技巧
    Ubuntu 16.04安装sublime text3
    GPU运行Tensorflow的几点建议
    挂载共享文件夹
    ubuntu 用管理员身份进入系统
  • 原文地址:https://www.cnblogs.com/wadmwz/p/7822340.html
Copyright © 2011-2022 走看看