办法是使用java.util.concurrent包下的计数器工具类CountDownLatch,看demo,本demo巧妙的使用了CountDownLatch让多线程模拟高并发以及所有线程执行完之后的耗时。
private static Date startDate;
private static Date endDate;
private static String startTime;
private static String endTime;
/**
* 测试memcached高并发,主函数 .
*
* @param args
*/
public static void main(String args[]) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
memcachedManager = ctx.getBean(MemcachedManager.class);
startDate=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmssSSS");
startTime=format.format(startDate);
//顺序执行
//executeOrder();
//并发执行
executeConcurrent();
}
/**
* 顺序执行
*/
public static void executeOrder()
{
int total=10000;
for (int a = 0; a < total; a++) {
try {
// 顺序执行
memcachedManager.put("hello" + a, "Hello,xmemcached" + a);
String value1 = memcachedManager.get("hello" + a);
System.out.println("第" + a + "个请求,hello" + a + "=" + value1);
} catch (Exception e) {
e.printStackTrace();
}
}
endDate=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmssSSS");
endTime=format.format(endDate);
long consumeTime=endDate.getTime()-startDate.getTime();
System.out.println("顺序执行"+total+"次,开始时间:"+startTime+",结束时间"+endTime+",总耗时:"+consumeTime+"毫秒");
}
/**
* 高并发执行
*/
public static void executeConcurrent()
{
int cycleTimes=100;//循环次数
int concurrentTimes=100;//同时并发次数
CountDownLatch countDownLatchTotal = new CountDownLatch(cycleTimes*concurrentTimes);
//循环次数
for (int m = 0; m < cycleTimes; m++) {
CountDownLatch countDownLatch = new CountDownLatch(1);
//并发执行线程个数
for (int a = 0; a < concurrentTimes; a++) {
try {
// 多线程同时执行
new Thread(new MyRun(countDownLatchTotal,countDownLatch, a)).start();
} catch (Exception e) {
e.printStackTrace();
}
}
// 同时启动多个线程
countDownLatch.countDown();
}
try {
//在所有线程未执行完,即countDownLatchTotal计数还未到0的情况下会继续等在这里
countDownLatchTotal.await();
endDate=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmssSSS");
endTime=format.format(endDate);
long consumeTime=endDate.getTime()-startDate.getTime();
System.out.println("并发执行"+(cycleTimes*concurrentTimes)+"次,开始时间:"+startTime+",结束时间"+endTime+",总耗时:"+consumeTime+"毫秒");
//所以线程执行完了,再计算总耗时
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* * 线程类
*
*
*/
public static class MyRun implements Runnable {
private CountDownLatch countDownLatch;
private CountDownLatch countDownLatchTotal;
public int index;
public MyRun(CountDownLatch countDownLatchTotal,CountDownLatch countDownLatch, int index) {
this.countDownLatchTotal=countDownLatchTotal;
this.countDownLatch = countDownLatch;
this.index = index;
// threadLocal.set(index);
}
@Override
public void run() {
try {
System.out.println("当前等待线程:" + index);
//计数器未到达0的时候,每个线程都先等在这里
countDownLatch.await();
System.out.println("所有线程同时开始执行" + index);
memcachedManager.put("hello" + index, "Hello,xmemcached" + index);
String value1 = memcachedManager.get("hello" + index);
System.out.println("当前线程名字:" + Thread.currentThread().getName() + ",第" + index + "个请求,hello" + index
+ "=" + value1);
//执行完一次计数器减一
countDownLatchTotal.countDown();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Error er) {
er.printStackTrace();
}
}
}