0; response.close(); httpclient.close(); //返回响应 } 捕获(例外e){ e、 printStackTrace(); },java,multithreading,servlets,apache-httpclient-4.x,Java,Multithreading,Servlets,Apache Httpclient 4.x" /> 0; response.close(); httpclient.close(); //返回响应 } 捕获(例外e){ e、 printStackTrace(); },java,multithreading,servlets,apache-httpclient-4.x,Java,Multithreading,Servlets,Apache Httpclient 4.x" />

Java多线程HttpClient-4.3.3问题 试试看{ //使用其类的url创建新的httpPost请求 HttpPost HttpPost=新的HttpPost(“http://192.168.1.229:8080/flightcache/flightcache" ); //创建参数并将其添加到httpPost List paramList=new ArrayList(); 添加(新的BasicNameValuePair(“json_req”,格式)); UrlEncodedFormEntity formEntity=新的UrlEncodedFormEntity(参数列表); httpPost.setEntity(formEntity); //执行请求并保存响应 CloseableHttpResponse response=httpclient.execute(httpPost,上下文); HttpEntity=response.getEntity(); for(标头:response.getAllHeaders()){ System.out.println(header.getName()+“:”+header.getValue()); } resp=entity.getContent().available()>0; response.close(); httpclient.close(); //返回响应 } 捕获(例外e){ e、 printStackTrace(); }

Java多线程HttpClient-4.3.3问题 试试看{ //使用其类的url创建新的httpPost请求 HttpPost HttpPost=新的HttpPost(“http://192.168.1.229:8080/flightcache/flightcache" ); //创建参数并将其添加到httpPost List paramList=new ArrayList(); 添加(新的BasicNameValuePair(“json_req”,格式)); UrlEncodedFormEntity formEntity=新的UrlEncodedFormEntity(参数列表); httpPost.setEntity(formEntity); //执行请求并保存响应 CloseableHttpResponse response=httpclient.execute(httpPost,上下文); HttpEntity=response.getEntity(); for(标头:response.getAllHeaders()){ System.out.println(header.getName()+“:”+header.getValue()); } resp=entity.getContent().available()>0; response.close(); httpclient.close(); //返回响应 } 捕获(例外e){ e、 printStackTrace(); },java,multithreading,servlets,apache-httpclient-4.x,Java,Multithreading,Servlets,Apache Httpclient 4.x,我试图同时向Servlet发送多个HttpPost请求,但只有一个执行上述代码的线程接收到响应。我检查了我的Servlet,但是响应写得正确httpClient创建如下 poolighttpclientconnectionmanager cm=new-poolighttpclientconnectionmanager(); CloseableHttpClient httpClient=HttpClients.custom().setConnectionManager(cm.build(); 谁

我试图同时向Servlet发送多个HttpPost请求,但只有一个执行上述代码的线程接收到响应。我检查了我的Servlet,但是响应写得正确<代码>httpClient创建如下

poolighttpclientconnectionmanager cm=new-poolighttpclientconnectionmanager();
CloseableHttpClient httpClient=HttpClients.custom().setConnectionManager(cm.build();
谁能帮我解释一下为什么只有一个线程收到响应

提前感谢

publicstaticvoidmain(字符串[]args)引发异常{
FileUtil.init();
PoolightPClientConnectionManager cm=新的PoolightPClientConnectionManager();
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(200);
CloseableHttpClient httpClient=HttpClients.custom().setConnectionManager(cm.build();
HTTPThread.THREAD_计数=2;
HTTPThread.start=新的CountDownLatch(HTTPThread.THREAD\u COUNT);
线程[]线程=新线程[HTTPThread.Thread_COUNT];
对于(int i=0;i
HTTPThread类:

public HTTPThread( CloseableHttpClient httpclient ){
    this.httpclient = httpclient;
    context = HttpClientContext.create();
}

public void run(){
    String format = randomRequest();

    start.countDown();

    try{
        start.await();
    }
    catch( InterruptedException e ){
        e.printStackTrace();
    }

    boolean resp = false;
    long timeMillis = System.currentTimeMillis();
    try{

        // create new httpPost request with url of his class
        HttpPost httpPost = new HttpPost( "http://192.168.1.229:8080/flightcache/flightcache" );

        // create params and add it to httpPost
        List<NameValuePair> paramList = new ArrayList<NameValuePair>();
        paramList.add( new BasicNameValuePair( "json_req", format ) );
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity( paramList );
        httpPost.setEntity( formEntity );

        // execute request and save response
        CloseableHttpResponse response = httpclient.execute( httpPost, context );

        HttpEntity entity = response.getEntity();
        resp = entity.getContent().available() > 0;

        response.close();
        httpclient.close();
        // return the response
    }
    catch( Exception e ){
        e.printStackTrace();
    }
    long end = System.currentTimeMillis() - timeMillis;

    if( !resp ){
        System.out.println( "Response was empty." );
    }

    if( end <= 0 ){
        times.add( 1L );
    }
    else{
        times.add( end );
    }

}
公共HTTPThread(可关闭httpclient httpclient){
this.httpclient=httpclient;
context=HttpClientContext.create();
}
公开募捐{
字符串格式=随机请求();
开始倒数();
试一试{
start.wait();
}
捕捉(中断异常e){
e、 printStackTrace();
}
布尔resp=false;
long-timeMillis=System.currentTimeMillis();
试一试{
//使用其类的url创建新的httpPost请求
HttpPost HttpPost=新的HttpPost(“http://192.168.1.229:8080/flightcache/flightcache" );
//创建参数并将其添加到httpPost
List paramList=new ArrayList();
添加(新的BasicNameValuePair(“json_req”,格式));
UrlEncodedFormEntity formEntity=新的UrlEncodedFormEntity(参数列表);
httpPost.setEntity(formEntity);
//执行请求并保存响应
CloseableHttpResponse response=httpclient.execute(httpPost,上下文);
HttpEntity=response.getEntity();
resp=entity.getContent().available()>0;
response.close();
httpclient.close();
//返回响应
}
捕获(例外e){
e、 printStackTrace();
}
long end=System.currentTimeMillis()-timeMillis;
如果(!resp){
System.out.println(“响应为空”);
}

如果(end通过代码挖掘,则在其他线程有机会连接之前,HttpClient已经关闭


因为它们都使用同一个客户端,所以HttpClient#close()应该在所有线程连接之后

您能显示处理线程的代码吗?我在上面发布了代码。这是一个简单的main方法,创建定义的线程计数并启动它们。啊,这是一个自定义可运行的-抱歉,此时还需要HTTPThread类,它是主要的罪魁祸首。您能更改自定义的sto吗p wait with thread joins?好的,我发布了更新的main方法并发布了类HTTPThreadsorry,它与线程无关-在线程连接和测试之后,你能移动httpclient.close()吗?