Java做到真正的并发【测试Http的POST并发请求】

原文链接:http://flysnowxf.iteye.com/blog/1188496

package com.multi.thread;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/***
 * 实现真正的并发同时执行,可通过CyclicBarrier来控制。
 * 
 * @ClassName TestPostByMultiThread

 */
public class TestPostByMultiThread {
	 public static void main(String[] args) {  
		 
         int count = 10;  
         CyclicBarrier cyclicBarrier = new CyclicBarrier(count);  
         ExecutorService executorService = Executors.newFixedThreadPool(count);  
         for (int i = 0; i < count; i++) { 
              executorService.execute(new TestPostByMultiThread().new Task(cyclicBarrier,i+1+"")); 
         }
 
         executorService.shutdown();  
         while (!executorService.isTerminated()) {  
              try {  
                   Thread.sleep(10);  
              } catch (InterruptedException e) {  
                   e.printStackTrace();  
              }  
         }  
    }  
 
    public class Task implements Runnable {  
         private CyclicBarrier cyclicBarrier; 
         private String userid;
 
         public Task(CyclicBarrier cyclicBarrier,String uid) {  
              this.cyclicBarrier = cyclicBarrier;  
              this.userid = uid;
         }  
 
         @Override  
         public void run() {  
              try {  
                   // ==========等待所有任务准备就绪  ====================
                   cyclicBarrier.await();
                   
                   // 并发测试内容  
                   System.out.println(new Date().toLocaleString()+"   :haha--------------------userid:"+this.userid);
                   executeMethod(this.userid);
                   
              } catch (Exception e) {  
                   e.printStackTrace();  
              }  
         }  
    }  
    
    public void executeMethod(String userid) throws ParseException, IOException{
    	System.out.println(new Date().toLocaleString()+" =====并发开始==="+userid);
    	String Result = "";
    	String Url = "http://sa.hkliop.com/api/getnums";
		// HttpPost连接对象
		HttpPost httpRequest = new HttpPost(Url);
		// 使用NameValuePair来保存要传递的Post参数
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("cnum", "96"));
		params.add(new BasicNameValuePair("id", userid));
			// 设置字符集
			HttpEntity httpEntity = null;
			try {
				httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			// 请求httpRequest
			httpRequest.setEntity(httpEntity);
			// 取得默认的HttpClient
			DefaultHttpClient httpClient = new DefaultHttpClient();
			// 取得HttpResponse
			HttpResponse httpResponse = null;
			try {
				httpResponse = httpClient.execute(httpRequest);
			} catch (IOException e) {
				e.printStackTrace();
			}
			// HttpStatus.SC_OK表示连接成功
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				// 取得返回的字符串
				Result = EntityUtils.toString(httpResponse.getEntity());
				System.out.println(new Date().toLocaleString()+"   返回结果:"+Result);
			} else {
			}
    }
    
}

	 
  

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页