代码拉取完成,页面将自动刷新
import com.google.gson.JsonObject;
import java.util.Date;
public class TestClientRunnable implements Runnable {
private TestClient testClient;
private RequestMethod method;
private long waitTime;
private JsonObject data;
private boolean running;
private int count;
private long totalTime;
private int failCount;
private long longestTime = 0;
private long shortestTime = 0x7fffffffffffffffL;
public TestClientRunnable(String url, RequestMethod method, long waitTime){
this(url, method, waitTime, new JsonObject());
}
public TestClientRunnable(String url, RequestMethod method, long waitTime, JsonObject data){
testClient = new TestClient(url);
this.method = method;
this.waitTime = waitTime;
this.data = data;
running = true;
}
public int getCount(){
return count;
}
public int getFailCount(){
return failCount;
}
public long getTotalTime(){
return totalTime;
}
public long getLongestTime() {
return longestTime;
}
public long getShortestTime() {
return shortestTime;
}
@Override
public void run() {
int counter;
//System.out.println(Thread.currentThread().getName() + " is running");
while (running){
counter = 1;
switch (method){
case GET:
if (testClient.get() == null)
counter = 0;
break;
case POST:
if (testClient.post(data) == null)
counter = 0;
break;
case PUT:
//TODO: PUT method
break;
case DELETE:
//TODO: GET method
break;
}
count += counter;
long dt = testClient.lastTime;
//System.out.println(dt);
longestTime = longestTime > dt ? longestTime : dt;
shortestTime = shortestTime < dt ? shortestTime : dt;
if (counter > 0){
totalTime += dt;
}
else {
failCount += 1;
}
try {
Thread.sleep(waitTime - dt > 0 ? waitTime - dt : 0);
}
catch (InterruptedException e){
running = false;
//System.out.println(Thread.currentThread().getName() + " is exiting, sent " + count + " requests");
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。