java模拟并发测试

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;


import net.sf.json.JSONObject;


import org.apache.http.HttpResponse;

import org.apache.http.HttpStatus;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.util.EntityUtils;


/**

 * ClassName: HttpClientTest <br/>

 * Function: TODO ADD FUNCTION. <br/>

 * @version 

 * @since  JDK 1.6

 */

public class HttpClientTest {

  private static int thread_num = 200; // 线程数

    private static int client_num = 500; // 执行程序次数

  

    public static void main(String[] args) {  

         DefaultHttpClient httpclient = new DefaultHttpClient();

        ExecutorService exec = Executors.newCachedThreadPool();  

  

        final Semaphore semp = new Semaphore(thread_num);  

  

        for (int index = 0; index < client_num; index++) {  

  

            final int NO = index;  

  

            Runnable run = new Runnable() {  

                public void run() {  

                    try {  

                        semp.acquire();  

                        System.out.println("Thread:" + NO);

                        

                        /*---------------------具体业务逻辑 begin------------------------*/

                        String host = "";


                        HttpPost post = new HttpPost(host);

                        

                        //配置json参数

                        JSONObject params = new JSONObject();  

                        params.put("aa", "11");  

                        params.put("bb", "22");  

                        params.put("cc", "33"+NO);

                        StringEntity s = new StringEntity(params.toString());

                        

                        //设置编码格式

                        s.setContentEncoding("UTF-8");

                        //发送json数据需要设置contentType

                        s.setContentType("application/json");  

                        post.setEntity(s);  

                        HttpResponse res = httpclient.execute(post); 

                        

                        //httpstatus = 200

                        if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  

                            String result = EntityUtils.toString(res.getEntity());// 返回json格式:

                            JSONObject response = null;  

                            response = JSONObject.fromObject(result);

                            //输出返回结果

                            System.out.println(response.toString());

                        }

                        //释放链接

                        EntityUtils.consume(res.getEntity());

                        

                        /*---------------------具体业务逻辑 end------------------------*/

                        

                        semp.release();  

                    } catch (Exception e) {  

                        e.printStackTrace();  

                    }  

                }  

            };  

            exec.execute(run);  

        }  

        exec.shutdown();  

    }

}


转载于:https://my.oschina.net/u/1259194/blog/351487

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值