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
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
req.put("username","little");
req.put("password","1992");
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();
}
}