Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

java - How to make httpclient safe thread when serval requset call the execute at the same time?

I define the httpclient like this way,when serval requset call the execute method,I find that some method retuslt is the same,How to fix this bug? Now I want to lock the execute method.It that right?

   //defind global variable:client
   Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", new PlainConnectionSocketFactory())) .build();
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg); 
        client = HttpClients.custom()
                .setConnectionManager(cm).build();

     //call method
      synchronized (this) {
            HttpResponse response = client.execute(get);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 
                  System.out.println(EntityUtils.toString(response.getEntity())); 
            } 
        }
 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...