测试结果:
代码如下:
public static void main(String[] args) throws InterruptedException {
//开始时间
long start = System.currentTimeMillis();
int threadNum = 10;
//启动CountDownLatch,定义计数器数量
final CountDownLatch count = new CountDownLatch(threadNum);
//相当于创建了10个线程,每个线程调用100万次
for (int i = 0; i < threadNum ; i++) {
//创建多线程,采用内部类
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
// Object o = SingletonLHan.getSingleton();
// Object o = SingletonEHan.getSingleton();
// Object o = SingletonDouble.getSingleton();
// Object o = SingletonStatic.getSingleton();
Object o = SingletonEnum.INSTANCE;
}
//一个线程执行完后,计数器数量减一,知道0为止
count.countDown();
}
}).start();
}
// main线程阻塞,直到计数器为0,才会继续往下执行
count.await();
//结束时间
long end = System.currentTimeMillis();
System.out.println("总耗时:" + (end - start));
}