67,514
社区成员
发帖
与我相关
我的任务
分享
public class Test1 {
public static int success = 0;
public static int fail = 0;
public static int reject = 0;
public static void main(String[] args) throws Exception {
long a = System.currentTimeMillis();
ThreadDemo[] td = new ThreadDemo[7000];
for(int i=0;i<td.length;i++){
td[i] = new ThreadDemo(i,a);
}
for(int i=0;i<td.length;i++){
td[i].start();
}
}
public static synchronized void setValue(int s,int f,int r){
success+=s;
fail+=f;
reject+=r;
}
public static class ThreadDemo extends Thread {
private int id = 0;
private long s = 0;
public ThreadDemo(int id,long s){
this.id=id;
this.s=s;
}
@Override
public void run() {
int success = 0;
int request_fail = 0;
int request_reject = 0;
URL url;
for (int i = 0; i < 100; i++) {
try {
url = new URL("http://ip:8080/Project1/test.jsp");
InputStream is = url.openStream();
byte[] b = new byte[100];
StringBuffer sb = new StringBuffer();
while(is.read(b)!=-1){
String ss = new String(b,"utf-8");
if(!"".equals(ss.trim()))
sb.append(ss.trim());
}
is.close();
success++;
} catch (ConnectException e) {
request_fail++;
}catch(SocketException ee){
request_reject++;
}catch(Exception e){
e.printStackTrace();
}
}
Test1.setValue(success,request_fail,request_reject);
System.out.println("request --" + id + "-- url end.take "
+ (System.currentTimeMillis() - s) + "ms success="+Test1.success+" fail="+Test1.fail+" request_reject="+Test1.reject);
}
}
}