package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class ConcurrentTest {
private static int thread_num =30;// 200;
private static int client_num = 30;// 460;
public static void main(String[] args) {
long start = System.currentTimeMillis();
ExecutorService exec = Executors.newCachedThreadPool();
// thread_num个线程可以同时访问
final Semaphore semp = new Semaphore(thread_num);
// 模拟2000个客户端访问
for (int index = 0; index < client_num; index++) {
final int NO = index;
exec.execute(new TaskThread(semp, NO));
}
long timeSpend = (System.currentTimeMillis()-start)/1000;
System.out.println( "花费1: "+timeSpend +"秒" );
// 退出线程池
exec.shutdown();
timeSpend = System.currentTimeMillis()-start;
System.out.println( "花费2: "+timeSpend +"秒" );
//long end = (System.currentTimeMillis()-start)/1000;//当前时间离当天0点的毫秒数
}
static class TaskThread implements Runnable {
Semaphore semp;
int NO;
public TaskThread(Semaphore semp, int NO) {
this.semp = semp;
this.NO = NO;
}
@Override
public void run() {
try {
// 获取许可
semp.acquire();
System.out.println("Thread:" + NO);
// String host =
// "http://10.99.23.42:7001/KMQueryCenter/query.do?";
// String para = "method=getQueryResult&pageNum=1&pageSize=5&" +
// "queryKeyWord=" + getRandomSearchKey(NO)
// + "&questionID=-1&questionIdPath=-1&searchType=1" +
// "&proLine=&proSeries=&proType=" + NO;
// http://192.168.2.115:8080/revitbus/revit/list.html?&tagId=12-&type=0&system=1&year=2016
String host = "http://192.168.2.10:8080/revitbus/revit/list.html?";
String para = "&type=0&system=1&year=2016";
System.out.println(host + para);
URL url = new URL(host);// 此处填写供测试的url
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Proxy-Connection", "Keep-Alive");
connection.setDoOutput(true);
connection.setDoInput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.print(para);
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
String result = "";
while ((line = in.readLine()) != null) {
result += line;
}
// System.out.println(result);
// Thread.sleep((long) (Math.random()) * 1000);
// 释放
System.out.println("第:" + NO + " 个");
//System.out.println(result);
semp.release();
} catch (Exception e) {
e.printStackTrace();
}
};
}
}
输出结果两类 :
线程启动:
Thread:0
Thread:5
Thread:4
Thread:2
Thread:7
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:3
Thread:1
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:8
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:6
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:10
Thread:9
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:11
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:12
Thread:13
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:14
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:15
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:16
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:17
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:18
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:20
Thread:19
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:21
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:22
Thread:23
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:26
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:24
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:25
Thread:27
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:28
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:30
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:29
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:31
Thread:34
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:32
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:36
Thread:35
Thread:37
Thread:33
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:38
Thread:39
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:40
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:41
Thread:42
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:44
Thread:43
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:46
Thread:45
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:47
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:48
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:50
Thread:51
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:54
Thread:55
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:59
Thread:58
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:49
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:53
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:52
Thread:57
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
Thread:56
http://192.168.2.10:8080/revitbus/revit/list.html?&type=0&system=1&year=2016
输出结果 类型1 失败
java.io.IOException: Server returned HTTP response code: 500 for URL: http://192.168.2.10:8080/revitbus/revit/list.html?
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)
at test.ConcurrentTest$1.run(ConcurrentTest.java:68)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
输出结果 类型2 成功
第:11 个
<!DOCTYPE html><html><head> <meta charset="utf-8"><meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="keywords" content="revit,revitbus,RevitBus,bim,BIM" /><meta name="description" content="RevitBus—基于AUTODESK Revit软件的二次开发平台" /><title>RevitBus</title><link rel="shortcut icon" href="/revitbus/resources/images/Revit.png" type="image/x-icon"><link rel="stylesheet" type="text/css" href="/revitbus/resources/css/style.css"><script type="text/javascript" src="/revitbus/resources/thirdparty/jquery-1.9.1.min.js"></script> <meta charset="UTF-8"> <meta http-e
----省略后面 打出了 整个界面
类型1 33个 类型2 27个
也可以 调整线程个数从10 20 30往上增加当出现错误的时候就是上限 比如 这个程序并发 大致是27 除以主程序里面的end-start 的时间差
估计10个左右并发
参考http://blog.csdn.net/a9529lty/article/details/8492607