package com.wxprogram;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 调用方
* 实现回调接口
*/
public class Caller implements CallBack {
//用于计数
private int length, count;
private static List<Long> list = new ArrayList<Long>();
/**
* 构造函数
*/
public Caller() {
length = 10;
count = 0;
}
/**
* 构造函数
*/
public Caller(int len) {
this.length = len;
}
/**
* 要调用的Deal方法的函数,模拟length条线程进行处理
* 总线程数length
*/
public void deal(int finalI) {
Executor dev = new Executor();
for (int i = 0; i < length; i++) {
new Thread(new Runnable() {
@Override
public void run() {
//调用具体逻辑,传入此对象
dev.deal(Caller.this, finalI,list);
}
}).start();
}
}
/**
* 回调函数处理逻辑,等所有线程处理完,进行后续逻辑处理
*
* @param result
*/
public void call() {
synchronized (Caller.class) {
//输出执行状态
// System.out.println(result);
length--;
count++;
System.out.println("执行线程数量统计=" + count);
if (length == 0) {
System.out.println("所有线程执行完毕"+list.size());
Collections.sort(list);
long requestTimeCounts =0L;
for (Long l : list) {
requestTimeCounts+=l;
}
long avgrequestTime = requestTimeCounts/100;
System.out.println("请求URL:http://www.baidu.com 并发数:"+10+" 请求次数"+list.size()+" 响应总时长:"+requestTimeCounts+" 平均响应时长:"+avgrequestTime);
int j = list.size()%95;
long requestTimeCountsPer95 = 0;
for(int i=0;i<j;i++) {
requestTimeCountsPer95+=list.get(i);
}
System.out.println("95%响应时长为:"+requestTimeCountsPer95/j);
}
}
}
}
评论 (1 条评论)