第一个类:主函数类。Test.java
public static void main(String[] args) throws IOException {
int usersNum=1;//用户数
int orderNum=1;//单个用户发的订单数
Long per=1000l;
for (int i = 0; i < usersNum; i++) {
ChatThread ct = new ChatThread(i,orderNum,per);
}
}
第二个类:进程类ChatThread.java
public class ChatThread extends Thread {
private int userNum;//用户数
private long per;//间隔时间 1000l为一秒
private int totalCount;//用户请求订单数
public ChatThread(int userNum,int totalCount,Long per) {
super();
this.userNum = userNum;
this.totalCount = totalCount;
this.per = per;
this.start();
}
public void run() {
try {
startOrder();
} catch (Exception e) {
e.printStackTrace();
}
}
public void startOrder() throws Exception{
System.out.println("用户"+userNum+"开始提交订单");
long startTime = 0l; // 获取开始时间
long endTime=0l;// 获取结束时间
long totalCountTime=0l;
int cuowu9=0;
for (int i = 0; i < totalCount; i++) {
sleep(per);
startTime = System.currentTimeMillis(); // 获取开始时间
HttpClient httpclient = new HttpClient();
String url = "";//接口URL
GetMethod getMethod = new GetMethod(url);
httpclient.executeMethod(getMethod);
String result = getMethod.getResponseBodyAsString();
System.out.println("result:"+result);
String rr = result.substring(
result.indexOf("<ReturnCode>") + 12,
result.indexOf("</ReturnCode>"));
System.out.println("进程"+i+":::"+rr);
if("-9".equals(rr)){
cuowu9++;
}
getMethod.releaseConnection();
endTime = System.currentTimeMillis(); // 获取结束时间
}
}
}