我想同時執行五次cmd命令。所以我創建了一個線程,它可以執行命令並啓動線程五次。它是否正確 ?同時啓動一個線程5次?
MyRunnable r1 = new MyRunnable();
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 5; i++) {
executor.execute(r1);
}
.......
@Override
public void run()
{
try {
// Execute command
String command = "cmd /c start cmd.exe";
Process child = Runtime.getRuntime().exec(command);
// Get output stream to write from it
OutputStream out = child.getOutputStream();
out.write("cd C:/ /r/n".getBytes());
out.flush();
out.write("dir /r/n".getBytes());
out.close();
} catch (IOException e)
{
}
}
你的問題是什麼? – mvd
我不相信。執行時間本身需要時間。如果我沒有錯,開始一個正常的線程需要約2秒 – Amirag
那麼n次執行線程的正確方法是什麼? – seriously