@Test
public void testCreateCommentsThreads() throws Throwable{
//Runner数组,相当于并发多少个线程。
TestRunnable[] trs = new TestRunnable [100];
for(int i=0;i<100;i++){
trs[i]=new createCommentsThread();
}
// 执行多线程测试用例的Runner
MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
// 执行所有线程
mttr.runTestRunnables();
}
// 自定义的线程
private class createCommentsThread extends TestRunnable {
@Override
public void runTest() throws Throwable {
// 测试内容
createComments();
}
}
// 测试方法
public void createComments() throws Exception {
}
TestRunnable.class创建自定义测试线程
MultiThreadedTestRunner.class执行多线程测试用例的Runner
public MultiThreadedTestRunner(TestRunnable[] tr) {
this(tr, (TestRunnable[])null);
}
runTestRunnables方法执行线程
public void runTestRunnables(long maxTime) throws Throwable {
Thread.interrupted();
this.exception = null;
this.coreThread = Thread.currentThread();
this.threadGroup = new ThreadGroup(THIS_CLASS_NAME);
this.threadsFinished = false;
Thread[] monitorThreads = this.setupThreads(this.threadGroup, this.monitors);
Thread[] runnerThreads = this.setupThreads(this.threadGroup, this.runners);
boolean threadsStillRunning;
try {
threadsStillRunning = this.joinThreads(runnerThreads, maxTime);
} catch (InterruptedException var21) {
threadsStillRunning = true;
} finally {
Object var9 = this.synch;
synchronized(var9) {
if(!this.threadsFinished) {
this.interruptThreads();
} else {
LOG.debug("All threads finished within timeframe.");
}
}
}
if(threadsStillRunning) {
LOG.debug("Halting the test threads.");
this.setTimeoutError(maxTime);
try {
this.joinThreads(runnerThreads, this.maxFinalJoinTime);
} catch (InterruptedException var20) {
;
}
int killCount = this.killThreads(runnerThreads);
if(killCount > 0) {
LOG.fatal(killCount + " thread(s) did not stop themselves.");
this.setTimeoutError(this.maxFinalJoinTime);
}
}
LOG.debug("Halting the monitor threads.");
try {
this.joinThreads(monitorThreads, this.maxFinalJoinTime);
} catch (InterruptedException var19) {
;
}
this.killThreads(monitorThreads);
if(this.exception != null) {
LOG.debug("Exception occurred during testing.", this.exception);
throw this.exception;
} else {
LOG.debug("No exceptions caused during execution.");
}
}