wait until all threads finish their work in java

The approach I take is to use an ExecutorService to manage pools of threads. ExecutorService es = Executors.newCachedThreadPool(); for(int i=0;i<5;i++) es.execute(new Runnable() { /* your task */ }); es.shutdown(); boolean finished = es.awaitTermination(1, TimeUnit.MINUTES); // all tasks have finished or the time has been reached.

How do I make a delay in Java?

If you want to pause then use java.util.concurrent.TimeUnit: TimeUnit.SECONDS.sleep(1); To sleep for one second or TimeUnit.MINUTES.sleep(1); To sleep for a minute. As this is a loop, this presents an inherent problem – drift. Every time you run code and then sleep you will be drifting a little bit from running, say, every second. If this … Read more

Waiting for multiple SwingWorkers

I intend to remove all of the labels together when all of the workers have completed their tasks. As described here, a CountDownLatch works well in this context. In the example below, each worker invokes latch.countDown() on completion, and a Supervisor worker blocks on latch.await() until all tasks complete. For demonstration purposes, the Supervisor updates … Read more

Is there no way to Wait in C#?

Outside of ReadLine which you are already using that waits for user input, Thread.Sleep as in below is one basic method of stopping the application for X amount of time to wait for an execution to finish or a user to be ready. Thread.Sleep blocks execution though in more complex applications: Thread.Sleep(2000); Getting into an … Read more