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
- java线程池newCachedThreadPool
- Java newCachedThreadPool 创建线程池
- Java线程池newSingleThreadExecutor newFixedThreadPool newCachedThreadPool newScheduledThreadPool
- java线程池(四) Executors.newCachedThreadPool
- 线程池ExecutorService newCachedThreadPool()
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Java线程池(newCachedThreadPool、newFixedThreadPool、newScheduledThreadPool 、newSingleThreadExector )
- JAVA线程池 newSingleThreadExecutor,newFixedThreadPool,newCachedThreadPool,newScheduledThreadPool(一)
- java线程池以及newCachedThreadPool使用过程中的问题
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- 浅谈Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- 缓存线程池-newCachedThreadPool (搬迁至此)
- newfixedthreadpool线程池 与newCachedThreadPool 的区别
- 使用无限大小线程池 newCachedThreadPool 可能遇到的问题
- Executors 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- 201509——hulu笔试题目——rainfall
- JAVA多线程实现的三种方式
- CMS垃圾收集器介绍
- 线段树查询2
- JavaScript高级程序设计之基本概念之关系操作符第3.5.6讲笔记
- java线程池newCachedThreadPool
- QT qDebug输出QString的中文乱码,直接输出“中文”不乱码,解决方法
- HTML中的块是什么?
- 全排序 java实现
- android学习各种bug(5)
- 列出一个文件夹下面的所有文件
- JavaScript高级程序设计之基本概念之相等操作符第3.5.7讲笔记
- 设置centos 7 开机不载入图形界面
- 记录一次定位死锁的问题