What is the difference between join and CountDownLatch?

You can only use Thread.join if you’re handling the threads yourself. Most people choose not to deal with the minutia of thread handling directly, and instead use an ExecutorService to handle it for them. ExecutorServices do not directly reveal how they are executing tasks, so you would have to use a CountDownLatch: (Assuming you don’t want to just shutdown the whole service, that is.)

ExecutorService service = Executors.newFixedThreadPool(5);
final CountDownLatch latch = new CountDownLatch(5);

for(int x = 0; x < 5; x++) {
    service.submit(new Runnable() {
        public void run() {
            // do something
            latch.countDown();
        }
    });
}

latch.await();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)