1

I have loop like:

for(final TestCase tc : tcPool) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        runTCCmd(tc.createJavaClass());
                    }
                }).start();

            }

How can I freeze(stop) loop on time while new Thread(...) executing??

I used ExecutorServicebefore but it did not stop thread execution:

private ExecutorService executorService = Executors.newSingleThreadExecutor();

this.executorService.submit(new Runnable() {
                    @Override
                    public void run() {
                       runTCCmd(tc.createJavaClass());
                    }
                });
java_user
  • 929
  • 4
  • 16
  • 39
  • 1
    Declare a flag in Thread say like boolean running = true;. Add while(runnig) in the run method and make running=false whenever you want – Ravindra babu Aug 31 '15 at 10:36
  • 2
    there is an answer for ExecutorService case: http://stackoverflow.com/a/1250655/1233521 – vzhilin Aug 31 '15 at 10:42

2 Answers2

2

Try

Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
        runTCCmd(tc.createJavaClass());
    }
});
t.start();
t.join();
Danil Gaponov
  • 1,413
  • 13
  • 23
  • 1
    If _this_ is what OP was actually about, then clearly the whole shebang should have been replaced with just `runTCCmd(tc.createJavaClass());` Why on earth would someone create another thread, then _unconditionally_ wait for that thread to die before going on? – Marko Topolnik Aug 31 '15 at 11:20
1

Why don't you just use single thread executor? It will execute the tasks sequentially.

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)
taegeonum
  • 81
  • 1
  • 6