zoukankan      html  css  js  c++  java
  • 线程安全的单例模式(有参and无参)

    /**
     * 线程安全的单例模式:
     *
     */
    package yxxy.c_023;

    import java.util.Arrays;

    public class Singleton {
     //无参
     private Singleton() {
      System.out.println("single");
     }
     
     private static class Inner {
      private static Singleton s = new Singleton();
     }
     
     public static Singleton getSingle() {
      return Inner.s;
     }
     
     //有参
     
     /**
       *  private static class Inner {
       *  private static Singleton getInstance (String s,int i) {
       *       return new Singleton(s, i);
       *  }
       *  }
       *
       *  public static Singleton getSingle(String s,int i) {
       *    return Inner.getInstance(s, i);
       *  }
       *
       */
     
     
     public static void main(String[] args) {
      Thread[] ths = new Thread[200];
      for(int i=0; i<ths.length; i++) {
       ths[i] = new Thread(()->{
        Singleton.getSingle();
       });
      }
      
      Arrays.asList(ths).forEach(o->o.start());
     }
     
    }

     可以用到实例化对象和固定值计算

  • 相关阅读:
    四则运算2
    进度条博客
    随机生成30道100以内的四则运算题
    构建之法阅读笔记01
    自我介绍
    c# dataGridView cell添加下拉框
    Winform Combobox.Items tooltip
    中医和红外(北京第一个工作)
    pdf修复
    c# 导出数据到excel
  • 原文地址:https://www.cnblogs.com/Dream--/p/8477991.html
Copyright © 2011-2022 走看看