Junit本是不支持多線程的,一個單元測試case主進程跑完,其餘new出來的線程都會GG思密達。此篇mark一份在Junit中執行多線程的方法。git
<!-- https://mvnrepository.com/artifact/net.sourceforge.groboutils/groboutils-core --> <dependency> <groupId>net.sourceforge.groboutils</groupId> <artifactId>groboutils-core</artifactId> <version>5</version> <scope>test</scope> </dependency>
@Slf4j public class DeviceMessageSolverTest extends JUnitBaseTest { /** * deviceInfoService <br> */ @Resource private DeviceInfoService deviceInfoService; /** * deviceMessageSolver <br> */ @Resource private DeviceMessageSolver deviceMessageSolver; /** * list <br> */ private static List<DeviceInfoBO> list; /** * appKeyArr <br> */ private final String[] appKeyArr = { "2e2b380a56e7464aa678294c2c345545", "e2c85a1d245844fd9bdb14f0d0fc868a", "fa58f5306eb64ce094fe65fec6261587" }; /** * Description: 測試併發修改設備上下線<br> * * @author xubin<br> * @taskId <br> * <br> */ @Test public void doStatusTest() { try { log.info("測試併發修改設備上下線"); DeviceInfoBO bo = new DeviceInfoBO(); bo.setAppKey(appKeyArr[0]); PageInfo<DeviceInfoBO> page1 = deviceInfoService.queryEntityListByPage(bo, 1, DigitConst.HUNDRED); bo.setAppKey(appKeyArr[1]); PageInfo<DeviceInfoBO> page2 = deviceInfoService.queryEntityListByPage(bo, 1, DigitConst.HUNDRED); bo.setAppKey(appKeyArr[2]); PageInfo<DeviceInfoBO> page3 = deviceInfoService.queryEntityListByPage(bo, 1, DigitConst.HUNDRED); list = page1.getRows(); list.addAll(page2.getRows()); list.addAll(page3.getRows()); log.info("已準備初始化數據:{} 條", list.size()); //線程數 int n = DigitConst.TEN; TestRunnable[] trs = new TestRunnable[n]; for (int i = 0; i < n; i++) { trs[i] = new DeviceMsgTaskRunnable(); } MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs); mttr.runTestRunnables(); } catch (Throwable e) { log.error("測試併發修改設備上下線異常", e); fail("測試併發修改設備上下線異常"); } } /** * <Description> 線程任務<br> * * @author xubin<br> * @version 1.0<br> * @taskId <br> * @CreateDate Nov 19, 2018 <br> */ class DeviceMsgTaskRunnable extends TestRunnable { /** * Description: 線程執行方法<br> * * @author xubin<br> * @taskId <br> * <br> */ @Override public void runTest() { long startTime = System.currentTimeMillis();while (true) { try { //組裝參數 DeviceInfoBO db = list.get((int) (Math.random() * list.size())); DeviceStatusDTO deviceStatusDTO = new DeviceStatusDTO(); deviceStatusDTO.setAppKey(db.getAppKey()); deviceStatusDTO.setTimeFlag(new Date().getTime()); deviceStatusDTO.setDeviceCode(db.getDeviceCode()); if (db.getIsOnline() == MainConstant.DEVICE_CURRENT_OFFLINE) { deviceStatusDTO.setStatus(true); } else { deviceStatusDTO.setStatus(false); } log.info(JSONObject.toJSONString(deviceStatusDTO)); deviceMessageSolver.doStatus(deviceStatusDTO); } catch (Exception e) { log.error("線程執行異常", e.getMessage()); } } } } }
關鍵代碼:多線程
1.class DeviceMsgTaskRunnable extends TestRunnable 2.TestRunnable[] trs = new TestRunnable[n]; 3.for (int i = 0; i < n; i++) { trs[i] = new DeviceMsgTaskRunnable(); } 4.MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs); 5.mttr.runTestRunnables();