java 多线程框架
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); } }