importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.HttpURLConnection;importjava.net.URL;importjava.util.HashMap;importjava.util.Map;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.Semaphore;publicclassConcurrentTest {privatestaticintthread_num=200;privatestaticintclient_num=460;privatestaticMap keywordMap=newHashMap();static{try{
InputStreamReader isr=newInputStreamReader(newFileInputStream(newFile("clicks.txt")),"GBK");
BufferedReader buffer=newBufferedReader(isr);
String line="";while((line=buffer.readLine())!=null) {
keywordMap.put(line.substring(0, line.lastIndexOf(":")),"");
}
}catch(Exception e) {
e.printStackTrace();
}
}publicstaticvoidmain(String[] args) {intsize=keywordMap.size();//TODO Auto-generated method stubExecutorService exec=Executors.newCachedThreadPool();//50个线程可以同时访问finalSemaphore semp=newSemaphore(thread_num);//模拟2000个客户端访问for(intindex=0; index
Runnable run=newRunnable() {publicvoidrun() {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;
System.out.println(host+para);
URL url=newURL(host);//此处填写供测试的urlHttpURLConnection connection=(HttpURLConnection) url
.openConnection();//connection.setRequestMethod("POST");//connection.setRequestProperty("Proxy-Connection",//"Keep-Alive");connection.setDoOutput(true);
connection.setDoInput(true);
PrintWriter out=newPrintWriter(connection
.getOutputStream());
out.print(para);
out.flush();
out.close();
BufferedReader in=newBufferedReader(newInputStreamReader(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+"个");
semp.release();
}catch(Exception e) {
e.printStackTrace();
}
}
};
exec.execute(run);
}//退出线程池exec.shutdown();
}privatestaticString getRandomSearchKey(finalintno) {
String ret="";intsize=keywordMap.size();//int wanna = (int) (Math.random()) * (size - 1);ret=(keywordMap.entrySet().toArray())[no].toString();
ret=ret.substring(0, ret.lastIndexOf("="));
System.out.println("\t"+ret);returnret;
}
}