java线程池newCachedThreadPool

1、newCachedThreadPool 测试注册应用实例。(模拟发送带参数请求json串)

package com.ninepoint.babystar.action.test;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

import net.sf.json.JSONObject;

import com.ninepoint.babystar.server.units.JsonUtil;
import com.ninepoint.babystar.server.units.Utils;
import com.ninepoint.babystar.test.action.HttpPostJson;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
 * 
 * @Description 测试注册功能
 * @author ChenMei
 * @date 2015-8-11 下午02:28:45 
 * @version 1.0
 */
public class RegisterActionTest extends TestCase{

	public RegisterActionTest(String testName) {
		// TODO Auto-generated constructor stub
		super(testName);
	}
	public static Test suite() {
		TestSuite suite = new TestSuite("Test for com.ninepoint.babystar.test.action");
		//$JUnit-BEGIN$
		suite.addTest(new RegisterActionTest("registerAction"));//注册
		//suite.addTest(new RegisterActionTest("getVerificationCode"));//获取验证码
		//$JUnit-END$
		return suite;
	}
	/**
	 * 注册并发测试
	 * register={"mobile":"","password":"123456","verification_code":"123456","regCode":"124565"}
	 */
	@org.junit.Test
    public static void registerAction() { 
		final String ADD_URL = "http://192.168.1.152:8080/register.action";
		 // 线程池
        ExecutorService exec = Executors.newCachedThreadPool();
        // 只能5个线程同时访问
        final Semaphore semp = new Semaphore(8);
		for(int i=0;i<101;i++){
			//final JSONObject obj = new JSONObject();
			final int NO = i;
			final Map<String, String> tables = getParams();//参数json串
			final JSONObject obj = JsonUtil.parseJson(tables.get(i+""), JSONObject.class);
			Runnable run = new Runnable() {
                public void run() {
                    try {
                        // 获取许可
                        semp.acquire();
                        System.out.println("Accessing: " + NO);
                        System.out.println(HttpPostJson.appadd(ADD_URL,obj)+"FK");
                        //availablePermits()指的是当前信号灯库中有多少个可以被使用
                        System.out.println("-----------------" + semp.availablePermits()); 
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally{
                    	// 访问完后,释放
                        semp.release();
                    }
                }
            };
            exec.execute(run);
		}
		try {
			Thread.sleep(100000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 退出线程池
        //exec.shutdown();
    } 
}
2、HttpPostJson类(模拟发送带参数action请求,请求参数格式为json格式)

package com.ninepoint.babystar.test.action;
import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import net.sf.json.JSONObject; 
public class HttpPostJson extends Thread{
	    //public static final String ADD_URL = "http://192.168.1.101:8080/babyCircleList.action"; 
		private static StringBuffer sb = new StringBuffer("");
	    public static String appadd(String ADD_URL,JSONObject obj) { 
	        try { 
	            //创建连接 
	            URL url = new URL(ADD_URL); 
	            HttpURLConnection connection = (HttpURLConnection) url 
	                    .openConnection(); 
	            connection.setDoOutput(true); 
	            connection.setDoInput(true); 
	            connection.setRequestMethod("POST"); 
	            connection.setUseCaches(false); 
	            connection.setInstanceFollowRedirects(true); 
	            connection.setRequestProperty("Content-Type", 
	                    "application/json"); 

	            connection.connect(); 

	            //POST请求 {"pagenow":"1"}
	            DataOutputStream out = new DataOutputStream( 
	                    connection.getOutputStream()); 
	           /* JSONObject obj = new JSONObject(); 
	            obj.element("pagenow", "1"); */
	            System.out.println("JSONObject------>" + obj.toString());
	            out.writeBytes(obj.toString()); 
	            out.flush(); 
	            out.close(); 

	            //读取响应 
	            BufferedReader reader = new BufferedReader(new InputStreamReader( 
	                    connection.getInputStream())); 
	            String lines; 
	            while ((lines = reader.readLine()) != null) { 
	                lines = new String(lines.getBytes(), "utf-8"); 
	                sb.append(lines); 
	            } 
	            //System.out.println(sb);
	            reader.close(); 
	            // 断开连接 
	            connection.disconnect(); 
	            return sb.toString();
	        } catch (MalformedURLException e) { 
	            // TODO Auto-generated catch block 
	            e.printStackTrace(); 
	        } catch (UnsupportedEncodingException e) { 
	            // TODO Auto-generated catch block 
	            e.printStackTrace(); 
	        } catch (IOException e) { 
	            // TODO Auto-generated catch block 
	            e.printStackTrace(); 
	        } 
	        return "fail";
	    } 

	    /*public static void main(String[] args) { 
	    	for(int i=0;i<100;i++){
				new Thread(){
					public void run(){
						for(int i=0;i<100;i++){
							System.out.println(appadd()+"FK");
							try {
								this.sleep(10000);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}.start();
			}
	    } */
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值