停止循環執行我有像循環:而線程執行
for(final TestCase tc : tcPool) {
new Thread(new Runnable() {
@Override
public void run() {
runTCCmd(tc.createJavaClass());
}
}).start();
}
我怎樣才能凍結時間(停止)循環而new Thread(...)
執行?
我用ExecutorService
之前,但它並沒有停止線程執行:
private ExecutorService executorService = Executors.newSingleThreadExecutor();
this.executorService.submit(new Runnable() {
@Override
public void run() {
runTCCmd(tc.createJavaClass());
}
});
在線程中聲明一個標誌,如boolean running = true ;.在運行方法中添加while(runnig),並在需要時運行= false假設 –
ExecutorService情況下有一個答案:http://stackoverflow.com/a/1250655/1233521 – vzhilin