packageedu.aeon.test;importjava.util.concurrent.CountDownLatch;importedu.aeon.model.singleton.HungrySingleton;importedu.aeon.model.singleton.LazySingleton;importedu.aeon.model.singleton.LazySingleton1;importedu.aeon.model.singleton.SingletonDCL;importedu.aeon.model.singleton.SingletonEnum;/*** 测试多线程环境下五种创建单例模式的调用效率
*
*@authoraeon
**/
public classTest {public static void main(String[] args) throwsException {long start =System.currentTimeMillis();int threadNum = 100;final CountDownLatch countDownLatch = newCountDownLatch(threadNum);for (int i = 0; i < threadNum; i++) {new Thread(newRunnable() {
@Overridepublic voidrun() {for (int i = 0; i < 1000000; i++) {/*//1.测试饿汉式
HungrySingleton hungrySingleton = HungrySingleton.getSinletonInstance();
//2.测试懒汉式
LazySingleton lazySingleton = LazySingleton.getLazySingletonInstance();
//3.测试双重检测锁
SingletonDCL singletonDCL = SingletonDCL.getInstance();
//4.测试静态内部类
LazySingleton1 lazySingleton1 = LazySingleton1.getLazySingletonInstance();
//5.测试枚举
SingletonEnum singletonEnum = SingletonEnum.singletonEnumInstance;*/}
countDownLatch.countDown();
}
}).start();
}
countDownLatch.await();//main线程阻塞,直到计数器变为0,才会继续往下执行!
long end =System.currentTimeMillis();
System.out.println("总耗时:" + (end -start));
}
}