package com.jidongcloud.singleton;
import java.util.concurrent.CountDownLatch;
/**
* ClassName:ThreadSafeTest
* PackageName:com.jidongcloud.singleton
* Description:
*
* @date:2020/7/15 11:46
* @author:robin
*/
public class ThreadSafeTest {
public static void main(String[] args) {
int count=200;
//发令枪
CountDownLatch latch = new CountDownLatch(count);
long start = System.currentTimeMillis();
for (int i = 0; i <count ; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
//阻塞,count=0时候,就会释放所有共享锁
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
//
Object obj = LazyOne.getInstance();
/**
* com.jidongcloud.singleton.LazyOne@1b84180e
* com.jidongcloud.singleton.LazyOne@2e80de3
* com.jidongcloud.singleton.LazyOne@2e80de3
* com.jidongcloud.singleton.LazyOne@2e80de3
* com.jidongcloud.singleton.LazyOne@1b84180e
*/
System.out.println(obj);
}
}).start();//每循环一次,启动一个线程
//每次启动一个线程 ,count--
latch.countDown();
}
long end= System.currentTimeMillis();
System.out.println("总耗时:"+(end -start));
}
}
模拟多线程访问(万箭齐发)的工具类
最新推荐文章于 2023-02-21 10:27:00 发布