long
time2 =
new
Date().getTime();
System.out.println(time2 - time1);
}
}
class
TestThread
implements
Runnable, 而不是用
extends
Thread, 的好处是独立设计一个类,
这个类还可以
extends
其它的
class
, 而不是单独的
extends
Thread. 另外一个好处是,可以把
处理方法放在各个不同的方法中,然后在
void
run()中调用,程序结构比较清晰。
程序工作如下:
在初始化一个 TestThread 实例的时候,接受一个 Parameter 参数(稍候介绍),并在线程启
动时,计算开始的时间,向目标机器发送请求包,接受目标机器的返回结果,再次计算
时间,并得到两次时间之差,这就是服务器的响应时间。
具体程序可以自己看懂,就不多说了。
class
Parameter {
URL url;
String[] key;
String[] value;
String method;
int
length =
0
;
public
void
addPair(String k, String v) {
Array.set(key, length, k);
Array.set(value, length, v);
length++;
}
}
是用来传递参数的一个类。参数是主程序从文件中读出来并存入这个类的一个对象里,然
后通过初始化 TestThread 传递给它的对象。
public
class
TestServer {
static
int
loopTimes =
500
;
public
Parameter readFromArgFile(String str){
FileInputStream fileInput;
BufferedReader br;
Parameter param =
new
Parameter();
try
{
fileInput =
new
FileInputStream(
new
File(str));
br =
new
BufferedReader(
new
InputStreamReader( fileInput ));
String line;
while
( (line = br.readLine()) !=
null
) {
if
( line.startsWith("URL") ==
true
&& line.indexOf("=") >=
3
) {
int
f = line.indexOf("=");