I am trying to run the below concurrency code:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class TestThread {

   public static void main(final String[] arguments) throws InterruptedException {
      ExecutorService executor = Executors.newSingleThreadExecutor();

      try {
         executor.submit(new Task());
         System.out.println("Shutdown executor");
         executor.shutdown();
         executor.awaitTermination(5, TimeUnit.SECONDS);
      } catch (InterruptedException e) {
         System.err.println("tasks interrupted");
      } finally {

         if (!executor.isTerminated()) {
            System.err.println("cancel non-finished tasks");
         }
         executor.shutdownNow();
         System.out.println("shutdown finished");
      }
   }

   static class Task implements Runnable {

      public void run() {

         try {
            int duration = 6;
            System.out.println("Running Task!");
            TimeUnit.SECONDS.sleep(duration);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }     
}

and the output is :

Shutdown executor
Running Task!
shutdown finished
cancel non-finished tasks
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at java.lang.Thread.sleep(Thread.java:340)
    at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
    at TestThread$Task.run(TestThread.java:34)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

According to the output it seems finally was not executed despite the try block getting completed. Am I right with the understanding? And why was the exception immediately thrown before going to the finally block?

According to the output it seems finally was not executed despite the try block getting completed.

? line 3 of the output appears to be the message from line 23 in the finally block so it looks to me like the finally is being executed.

Be careful when reading a single output that combines System.out and System.err. They are separtely buffered, and may not appear in the same order that they were generated.

? line 3 of the output appears to be the message from line 23 in the finally block so it looks to me like the finally is being executed.

Yes finally is executed but it gets executed after the lines after it are executed first. Can the program flow be backwards like this? First line 22 and 23 are executed and then 20 despite the try block getting complete ? Shouldn't the line 20 be executed and then 22 and 23?

Be careful when reading a single output that combines System.out and System.err. They are separtely buffered, and may not appear in the same order that they were generated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.