Singleton(单例模式)的使用和测试效率
来源:互联网 发布:淘宝宝妈法兰西 编辑:程序博客网 时间:2024/06/05 05:05
测试时一个一个试
/** * @version * @description */package cn.xasmall.example;/** * @author 26248 * */public class TestSingleton{ private String name=null;// 懒汉模式(存在线程安全问题) private static TestSingleton testSingleton=null; //饿汉模式(不存在线程安全问题)// private static TestSingleton testSingleton=new TestSingleton(); private TestSingleton() { }// 懒汉模式// public static TestSingleton getInstance() {// if(testSingleton==null)// testSingleton=new TestSingleton();// return testSingleton;// }// 饿汉模式// public static TestSingleton getInstance() {// return testSingleton;// } //解决懒汉模式线程安全问题// synchronized public static TestSingleton getInstance() {// if(testSingleton==null)// testSingleton=new TestSingleton();// return testSingleton;// } //double-check(双重检测) public static TestSingleton getInstance() { if(testSingleton!=null) { } else { synchronized (TestSingleton.class) { if(testSingleton==null) testSingleton=new TestSingleton(); } } return testSingleton; } public String getName() { return name; } public void setName(String name) { this.name=name; }}// //内部类//public class TestSingleton{// private static class SingleHoder{// private static final TestSingleton singleton=new TestSingleton();// }// private TestSingleton() {// // }// public static final TestSingleton getInstance() {// return SingleHoder.singleton;// }//}//枚举(不是很理解)//public enum TestSingleton{// INSTANCE;// public void leaveTheBuilding() {// // }//}
/** * @version * @description */package cn.xasmall.example;/** * @author 26248 * */public class TestThreadSingleton implements Runnable { public TestThreadSingleton() { } public void run() {// System.out.println(TestSingleton.INSTANCE.hashCode()); System.out.println(TestSingleton.getInstance().hashCode()); }}
/** * @version * @description */package cn.xasmall.example;/** * @author 26248 * */public class TestSingletonmain { //测试时间 public static void main(String[] args) throws Exception { long starttime=System.currentTimeMillis(); Thread[] threads=new Thread[100]; for(int i=0;i<100;i++) { threads[i]=new Thread(new TestThreadSingleton()); threads[i].start(); } for(Thread t:threads) t.join(); Thread.sleep(3000); long finishtime=System.currentTimeMillis(); System.out.println("线程测试完成!"); System.out.println("测试使用时间为:"+(finishtime-starttime)+"毫秒"); }}
阅读全文
0 0
- Singleton(单例模式)的使用和测试效率
- singleton单例模式的使用
- Android - 单例模式(singleton)的使用
- Android - 单例模式(singleton)的使用
- 使用VS2013 实现一个简单的单例模式singleton
- Objective-C使用单例(Singleton)模式
- 【Unity技巧】使用单例模式Singleton
- 【Unity技巧】使用单例模式Singleton
- 设计模式GOF23之单例模式效率测试与反射和反序列化问题的解决
- 单例(Singleton)模式
- singleton 单例模式
- Singleton单例模式
- Singleton(单例)模式
- Singleton 单例模式
- 单例模式(Singleton)
- 单例模式(Singleton)
- Singleton(单例)模式
- Singleton(单例)模式
- 教您用数学课件制作工具画椭圆
- 大众点评接口优惠卷,订单对接
- myeclipse一直Building workspace
- Linux ALSA声卡驱动之一:ALSA架构简介
- 【zookeeper】Zookeeper 开发者手册
- Singleton(单例模式)的使用和测试效率
- linux文件分割(将大的日志文件分割成小的)
- Android生成帮助文档
- red hat安装VMware-tools遇到的问题
- exynox裸机开发1——点亮LED
- Installation QT ROS 插件 for Ubuntu 16.04
- java 定制日期
- MediaRecorder
- Linux ALSA声卡驱动之二:声卡的创建