代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
java 多线程框架
作者:
/ 发布于
2021/6/1
/
826
java 多线程框架,在测试接口时,若服务启动,则调用接口,若无对外接口,则使用此框架进行测试。
package com.sogou.demeter.test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.Iterator; import java.util.Random; import java.util.concurrent.ConcurrentHashMap; import org.apache.log4j.Logger; import com.sogou.demeter.test.ShardMongoTestWithSpring; public class MyThread<testSingleThread> extends Thread { private static final Logger log = Logger.getLogger(MyThread.class); private int cycleTime;// 每线程执行周期 private int cycleInterval;// 每执行一次后等待间隔 public MyThread(int cycleTime, int cycleInterval) { this.cycleTime = cycleTime; this.cycleInterval = 2000; } int createRandom(int min, int max) { Random random = new Random(); return random.nextInt(max - min + 1) + min; } public static class MyThreadTest{ public static ConcurrentHashMap<String, String> ThreadMap = new ConcurrentHashMap<String, String>(); public static int ThreadCount=0; public static int GetMap(){ Iterator keys = ThreadMap.keySet().iterator(); int Count=0; while(keys.hasNext()){ Object key = keys.next();//key Object value = ThreadMap.get(key);//上面key对应的value Count+=Integer.valueOf(value.toString()); } return Count; } public static void SetMap(String threadId,String value) { ThreadMap.put(threadId, value); } } private int threadId; public void setThreadId(int threadId) { this.threadId = threadId; } public void run() { // System.out.println("Thread ID is:" + this.threadId); while (cycleTime > 0) { // System.out.println("线程循环次数: " + cycleTime); //log.info("begin"); //String path = "c:/config.txt"; // ReportAPIImplTest a = new ReportAPIImplTest(); // XReportSupportMgrImplTest a = new XReportSupportMgrImplTest(); //xreport测试 // CpcAPIImplTest b = new CpcAPIImplTest(); //Apollo API // App1 b= new App1(); FileOutputStream out = null; try { out = new FileOutputStream("D:/UncleAn.txt",true); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } PrintStream a=new PrintStream(out); long start = System.currentTimeMillis(); ShardMongoTestWithSpring.testSingleThread(this.threadId); // CpcAPIImplTest b = new CpcAPIImplTest(); long end = System.currentTimeMillis(); System.out.println((end-start)); System.setOut(a); cycleTime--; if (cycleTime==0){ MyThreadTest.ThreadCount++; // System.out.println("zzzzz"+MyThreadTest.ThreadCount); } //System.out.println(cycleTime); try { Thread.sleep(cycleInterval); } catch (InterruptedException e) { e.printStackTrace(); } } } } package com.sogou.demeter.test; import java.io.IOException; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import com.sogou.demeter.test.MyThread.MyThreadTest; public class ExecuteThread { private int launchInterval;// 每次启动线程时间间隔,单位:秒 private int launchCount;// 每次启动线程数量 private int launchTime;// 线程启动次数 private int cycleTime;// 每个线程执行周期 private int cycleInterval;// 每个线程每执行一次后等待间隔,单位:毫秒 /** * * @author Uncle An * @param 每隔launchInterval秒启动launchCount个线程 * ,共启动launchTime次即launchCount*launchTime个线程,每个线程循环cycleTime次, * 循环间隔为cycleInterval毫秒 * */ public ExecuteThread(int launchInterval, int launchCount, int launchTime, int cycleTime, int cycleInterval) { this.launchInterval = launchInterval; this.launchCount = launchCount; this.launchTime = launchTime; this.cycleTime = cycleTime; this.cycleInterval = cycleInterval; } /* 配置启动线程数量和频率 */ public void executeTest() { for (int i = 0; i < launchTime; i++)// launchTime*launchCount= { ExecutorService excutorService = Executors.newFixedThreadPool(launchCount); // 内层控制每隔多长时间启动几个线程的数量,即每几秒多少个并发线程 for (int j = 0; j < launchCount; j++) { MyThread myThread = new MyThread(cycleTime, j); excutorService.execute(myThread); } try { excutorService.shutdown(); excutorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); // System.out.println("一轮已经结束啦"); Thread.sleep(launchInterval * 1000);// 每次启动线程的间隔 } catch (InterruptedException e) { System.out.println("executeTest() Exception"); e.printStackTrace(); } } } public static void main(String args[]) throws IOException { /* * ExecuteThread et = new ExecuteThread(Integer.parseInt(args[0]), * Integer.parseInt(args[1]), Integer.parseInt(args[2]), * Integer.parseInt(args[3]), Integer.parseInt(args[4])); */ ExecuteThread et = new ExecuteThread(1, 1, 50,10,1);// 每隔2秒启动20个线程,共启动1次即20个线程,每个线程循环1次, // ExecuteThread et = new ExecuteThread(1, 1, 1, 1, 1); // 间隔10毫秒 et.executeTest(); //MyThreadTest.GetMap(); // System.out.println("zzzzzz"+MyThreadTest.GetMap()); // App1 app = new App1(); // app.run(); /* while (true){ // System.out.println("zzzzz"+MyThreadTest.ThreadCount); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (MyThreadTest.ThreadCount>=1){ Map<String, Integer> threadMap = ShardMongoTestWithSpring.threadMap; System.out.println(threadMap); break;} } */ // System.out.println(threadMap); } }
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3