Maximum number of threads open to 100 no problem
More than 101, there's a lot of connection failures.
Initial judgment is the problem of Tomcat's maximum connection number setting
Reset Tomcat Server.xml
After acceptcount=200, the maximum thread can open to 150 without any errors
ACCEPTCOUNT Specifies the number of requests placed in the queue
<connector connectiontimeout= "20000" port= "8080" protocol= "http/1.1" redirectport= "8443" acceptCount= "/>"
Java code
package com.ask.pressure;
import Java.io.BufferedReader;
import java.io.IOException;
import Java.io.InputStream;
import Java.io.InputStreamReader;
import Java.io.OutputStream;
import java.net.HttpURLConnection;
import Java.net.URL;
public class Askpressure {
private static String URL = "http://192.168.0.59:8080/ask/index.html";
private static Integer error = 0;
private static Integer threads = 150;
private static Long StartTime;
public static void Main (string[] args) {
workthread[] workthreads = new Workthread[threads];
for (int i = 0; i < threads; i++) {
Workthreads[i] = new Workthread ();
}
starttime = System.currenttimemillis ();
//system.out.printf ("Thread array initialization takes%d milliseconds \ n", (System.currenttimemillis ()-starttime));
for (int i = 0; i < threads; i++) {
Workthreads[i].start ();
}
//system.out.printf ("Connection failed:%d", error);
}
private static Class Workthread extends Thread {
public void Run () {
Long start = System.currenttimemillis ();
long end = 0;
try {
url u = new url (URL);
HttpURLConnection urlconn = (httpurlconnection) u.openconnection ();
urlconn.setusecaches (FALSE);
urlconn.setrequestproperty ("Content-type", "text/html; Charset=utf-8 ");
Urlconn.connect ();
//system.out.printf ("Small doctoral page code:% s\n", urlconn.getcontentencoding ());
InputStream is = Urlconn.getinputstream ();
StringBuffer buffer = new StringBuffer ();
Readtobuffer (Buffer,is);
end = System.currenttimemillis ();
} catch (Exception e) {
synchronized (Error) {
error + +;
}
E.printstacktrace ();
}
synchronized (threads) {
Threads--;
System.out.printf ("There are%d unfinished threads, time consuming%d milliseconds \ n", Threads, (System.currenttimemillis ()-starttime));
if (threads = = 0) {
System.out.printf ("Total time consuming:%d milliseconds \ n", (System.currenttimemillis ()-starttime));
System.out.printf ("Connection failure:%d\n", error);
}
}
}
public void Readtobuffer (StringBuffer buffer,inputstream is)
throws IOException {
String Line; Used to hold content read per row
BufferedReader reader = new BufferedReader (
new InputStreamReader (IS));
line = Reader.readline (); Read the first line
while (line!= null) {//If line is empty description finished reading
Buffer.append (line); Add read content to buffer
buffer.append ("\ n"); Add a newline character
line = Reader.readline (); Read Next line
}
}
}
}
还有9个未完线程,耗时1484毫秒
还有5个未完线程,耗时1547毫秒
还有6个未完线程,耗时1516毫秒
还有7个未完线程,耗时1516毫秒
还有8个未完线程,耗时1484毫秒
还有4个未完线程,耗时1547毫秒
还有3个未完线程,耗时1547毫秒
还有2个未完线程,耗时1563毫秒
还有1个未完线程,耗时1578毫秒
总耗时:1578毫秒
连接失败:0
还有0个未完线程,耗时1578毫秒
总耗时:1578毫秒
连接失败:0