统计多线程下程序运行总时间
程序员文章站
2022-03-06 22:46:15
...
package com.gpcsoft.hct.epp.egp.thread;
import com.gpcsoft.hct.epp.egp.HttpClientBailApi;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
/**
* @Auther: gaojp
* @Date: 2019/5/5 17:14
* @Description:
*/
public class ThreadTest {
public static Boolean printStatusInfo = false;
public void mutiThread(){
long start = Calendar.getInstance().getTimeInMillis();
//线程数量
int num =100;
Boolean printStatus =false;
//计数
CountDownLatch countDownLatch = new CountDownLatch(num);
//停车栅栏
CyclicBarrier cyclicBarrier = new CyclicBarrier(num);
for (int i = 0; i <num ; i++) {
String mobile ="200"+i;
String tag =mobile+"_"+ Calendar.getInstance().getTimeInMillis();
new Thread(new ThreadNum(countDownLatch, mobile,tag,cyclicBarrier,start,printStatus)).start();
countDownLatch.countDown();
}
}
private class ThreadNum implements Runnable {
private CountDownLatch countDownLatch;
private CyclicBarrier cyclicBarrier;
private long start;
private boolean printStatus;
private String mobile;
private String tag;
private ThreadNum(CountDownLatch countDownLatch, String mobile, String tag) {
this.countDownLatch = countDownLatch;
this.mobile = mobile;
this.tag = tag;
}
private ThreadNum(CountDownLatch countDownLatch, String mobile, String tag,CyclicBarrier cyclicBarrier) {
this.countDownLatch = countDownLatch;
this.mobile = mobile;
this.tag = tag;
this.cyclicBarrier =cyclicBarrier;
}
private ThreadNum(CountDownLatch countDownLatch, String mobile, String tag,
CyclicBarrier cyclicBarrier,boolean printStatus) {
this.countDownLatch = countDownLatch;
this.mobile = mobile;
this.tag = tag;
this.cyclicBarrier =cyclicBarrier;
this.printStatus = printStatus;
}
private ThreadNum(CountDownLatch countDownLatch, String mobile, String tag,
CyclicBarrier cyclicBarrier,long start,boolean printStatus) {
this.countDownLatch = countDownLatch;
this.mobile = mobile;
this.tag = tag;
this.cyclicBarrier =cyclicBarrier;
this.start = start;
this.printStatus = printStatus;
}
@Override
public void run() {
try {
//等待所有线程准备完毕后,同时访问接口
countDownLatch.await();
Map<String,String> req = new HashMap<String,String>();
req.put("username","little");
req.put("password","1992");
Iterator<String> item = req.values().iterator();
StringBuffer sb = new StringBuffer();
while (item.hasNext()){
sb.append(item.next());
}
System.out.println(sb.toString());
String url ="http://localhost:9090/shiro/loginUser";
HttpClientBailApi api = new HttpClientBailApi();
try {
String string = api.postStringApi(url, req);
System.out.println("result:::"+string.substring(0,10)+"___"+mobile+"-"+tag);
}catch (Exception ex){
ex.printStackTrace();
}
//等待线程运行完成
int awaitNum = cyclicBarrier.await();
long end = Calendar.getInstance().getTimeInMillis();
if(!printStatusInfo) {
printStatusInfo = true;
System.out.println(awaitNum + "运行时间::" + (end - start));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ThreadTest test = new ThreadTest();
test.mutiThread();
}
}
上一篇: java生产者消费者snippet
下一篇: 线程池常用几种方式