用来测承受压力的……慢慢玩
完整代码如下:
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class HttpCheckOK {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 测试的网站URL
final URL url = new URL("http://10.1.1.49:8888/api/card_serial?cardID=1234567890&secretKey=");
// 并发数量
final int concurrentNum = 3;
// 等待时间,单位是毫秒
final int wait = 10;
// 新建一个线程池
ExecutorService pool = Executors.newCachedThreadPool();
// while(true){
// 下面是执行循环的次数
for (int i = 0; i < concurrentNum;) {
// 新线程
pool.execute(new Runnable() {
@Override
public void run() {
try {
// 一直不停地跑
while (true) {
URLConnection connection = url.openConnection();
InputStream inStream = connection.getInputStream();
// 上面两句是打开url并且建立一个输入流
byte[] buff = new byte[89];
while ((inStream.read(buff)) != -1) {
// 上面那句就是拿来读数据的
try {
// 等待多久
Thread.sleep(wait);
// System.out.println(new String(buff));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(new String(buff, "UTF-8"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
}
解析都写上去了,需要的时候直接改开始的常量的数字就可以更改参数了