java线程池newCachedThreadPool

来源:互联网 发布:linux 切换root用户 编辑:程序博客网 时间:2024/05/18 17:02

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 stubsuper(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 blocke.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 blocke.printStackTrace();}}}}.start();}    } */}



0 0