1

I am new to Java multithreading so, my question may be unreasonable. Please help me understand how it works otherwise.

ExecutorService executor = Executors.newCachedThreadPool();

            List<Future<Boolean>> futures = new ArrayList<>();
            for (int i = 0; i < emailAddressList.size(); i++) {
                futures.add(executor.submit(new Verify(emailAddressList.get(i))));
            }           
            executor.shutdown();
            while(!executor.awaitTermination(3, TimeUnit.SECONDS));

            for (int i = 0; i < emailAddressList.size(); i++) {
                bufferedWriter.write(emailAddressList.get(i) + "|" + futures.get(i).get());
                bufferedWriter.newLine();
            }

            bufferedWriter.close();

In the awaitTermination loop, I have set the TimeOut to 3 seconds. What I am trying to achieve here, on this timeout, I want to terminate all the Threads which have been live for minimum 3 seconds. But how do I identify that?

Bharat Nanwani
  • 653
  • 4
  • 11
  • 27

1 Answers1

0

They are terminated automatically. Your loop is useless.

You're giving 3 seconds for the tasks to terminate, if the tasks haven't finished by then you can't do much except call shutdownNow() which attempts to forcibly stop the running threads. If you think that the tasks require more than three seconds, increase the timeout, but there's no use in looping.

In fact there's probably no use in waiting for all tasks to terminate. Future.get() will block if the result is not available, so you can just start looping through the results. Of course if Verify does some potentially dangerous operations (like long running network operations without timeouts), this can block everything. But that should be fixed in Verify, not in this code.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • I Could do that. But, shudowNow() also terminates all the jobs in the queue also right? including the tasks running, right? – Bharat Nanwani Nov 09 '17 at 10:54
  • It returns any tasks that were in the queue and didn't start executing. It will try to stop running tasks by interrupting the thread. Calling `shutdown()` will probably be enough, if you put a suitable timeout. If the timeout exceeds and there might be something wrong, then you need `shutdownNow()`. – Kayaman Nov 09 '17 at 10:56
  • I understand it returns what it didn't execute, Do you suggest, I then submit these unused tasks again to the Execute to work? – Bharat Nanwani Nov 09 '17 at 11:16
  • Unless you expect your `Verify` jobs to behave badly, I wouldn't use `awaitTermination` at all. You've already shut down the pool, you can move on to printing the emails as the futures finish. – Kayaman Nov 09 '17 at 11:23
  • The problem is the job that each Thread is going to execute that may take any time from 5 minutes to upto an hour. I don’t want any of my threads to continue with the execution for more than 8 minutes. What do you suggest I do? – Bharat Nanwani Nov 09 '17 at 11:30
  • For task based timeouts, see solutions here https://stackoverflow.com/questions/2758612/executorservice-that-interrupts-tasks-after-a-timeout – Kayaman Nov 09 '17 at 11:41