首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java多线程HttpClient-4.3.3问题

Java多线程HttpClient-4.3.3问题
EN

Stack Overflow用户
提问于 2014-06-06 07:54:53
回答 1查看 1.2K关注 0票数 1
代码语言:javascript
复制
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();
        for( Header header : response.getAllHeaders() ){
            System.out.println( header.getName() + ":" + header.getValue() );
        }
        resp = entity.getContent().available() > 0;

        response.close();
        httpclient.close();
        // return the response
    }
    catch( Exception e ){
        e.printStackTrace();
}

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

代码语言:javascript
复制
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

有人能帮我解释一下为什么只有一条线程收到回复吗?

提前感谢

代码语言:javascript
复制
public static void main( String[] args ) throws Exception{
    FileUtil.init();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal( 200 );
    cm.setDefaultMaxPerRoute( 200 );
    CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager( cm ).build();

    HTTPThread.THREAD_COUNT = 2;
    HTTPThread.start = new CountDownLatch( HTTPThread.THREAD_COUNT );

    Thread[] threads = new Thread[ HTTPThread.THREAD_COUNT ];

    for( int i = 0; i < HTTPThread.THREAD_COUNT; i++ ){
        threads[ i ] = new Thread( new HTTPThread( httpClient ) );
    }

    for( Thread thread : threads ){
        thread.start();
    }

    for( Thread thread : threads ){
        thread.join();
    }

    httpClient.close();

    System.out.println( "Average response time: " + calAverage( HTTPThread.times ) + " milliseconds." );
}

HTTPThread类:

代码语言:javascript
复制
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 );
    }

}
EN

Stack Overflow用户

回答已采纳

发布于 2014-06-06 10:05:32

深入挖掘您的代码,HttpClient在其他线程有机会启动连接之前就关闭了。

由于它们都使用相同的客户端,所以HttpClient#close()应该是在所有线程连接之后

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24076877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档