线程执行时停止循环执行

时间:2015-08-31 10:30:21

标签: java multithreading

我有循环:

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());
                    }
                });

2 个答案:

答案 0 :(得分:2)

尝试

(#200) Requires extended permission: read_stream

答案 1 :(得分:1)

为什么不使用单线程执行器? 它将按顺序执行任务。

private ExecutorService executorService = Executors.newSingleThreadExecutor();
for(final TestCase tc : tcPool) {
      executorService.submit(new Runnable() {
          @Override
          public void run() {
              runTCCmd(tc.createJavaClass());
          }
      });
}
executorService.shutdown();
executorService.awaitTermination(shutdownTimeout, TimeUnit.MILLISECONDS)