在我的程序中,我有一個n測試腳本的列表,我需要迭代列表並運行3個並行的測試腳本。爲了實現這一任務,我創建了尺寸3的線程池我的實現是像下面的線程池使用Threadpool運行多個進程的Java runtime.exec()
ExecutorService executor = Executors.newFixedThreadPool(3);
for (int threadpoolCount = 0; threadpoolCount < classNames.size(); threadpoolCount++) {
Runnable worker = new ProcessRunnable(classNames.get(threadpoolCount));
executor.execute(worker);
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
下面
是我的線程執行其中我在它執行與Maven命令的批處理文件
public void run() {
try {
System.out.println(testname);
System.out.println("Task ID : " + this.testname + " performed by " + Thread.currentThread().getName());
Process p = Runtime.getRuntime().exec("cmd /C Junit_runner.bat" + " " + testname);
p.waitFor();
Thread.sleep(5000);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
以下是我在控制檯中看到(我不是在啓動命令提示符,然後在後臺運行它)
com.selenium.test.testname1 任務ID:由池-1-線程1
com.selenium.test.testname1 任務ID進行com.selenium.test.testname1 :com.selenium.test.testname2 由pool-執行1線程2
com.selenium.test.testname1 任務ID:由池-1-線程3
進行com.selenium.test.testname3 執行暫停這裏,它沒有做任何事情,我不是sur e背後發生了什麼。我也交叉檢查批處理文件的工作正常。
運行'Junit_runner.bat'需要多長時間? – Jatin
需要2到3分鐘才能完成執行和termiante,她的e是我的批處理文件命令。 ** @ Echo off echo%1 call mvn -f pom。xml test -Dtest =%1 exit ** – Vigneshwaran