Future
that was sent to an ExecutorService
. I told them to look at a previous posts I had done on the subject. However, they insisted that this was different. So I took a look at the code. Alas, it was slightly different, but like most folks including me, they were too close to the problem to see the answer. I looked at it, and at first glance I thought something was askew, but it was not.
The code for this project can be downloaded here: runnable-example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | --- exec -maven-plugin:1.2.1: exec (default-cli) @ runnable-example --- Future cancelled? false Future done ? false Future cancelled? true Future done ? true Interrupted Sleeping... Sleeping... Sleeping... Sleeping... Sleeping... Sleeping... Sleeping... Sleeping... Sleeping... Interrupted Breaking out of run loop. |
Here is the
Runnable
and the main class to execute it:
MyRunnable.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.bluelotussoftware.examples.threads; /** * * @author John Yeary <jyeary@bluelotussoftware.com> * @version 1.0 */ public class MyRunnable implements Runnable { private int counter = 0 ; @Override public void run() { boolean running = true ; while (running) { try { Thread.sleep( 1000 ); System.out.println( "Sleeping..." ); } catch (InterruptedException e) { counter++; System.out.println( "Interrupted" ); if (counter > 1 ) { System.out.println( "Breaking out of run loop." ); running = false ; } } } } } |
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package com.bluelotussoftware.examples.threads; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** * * @author John Yeary <jyeary@bluelotussoftware.com> * @version 1.0 */ public class Main { public static void main(String[] args) throws InterruptedException { // Example #1 Old School // Thread t = new Thread(new MyRunnable()); // t.start(); // Thread.sleep(5000); // t.interrupt(); // Thread.sleep(2000); // t.interrupt(); // Example #2 ExecutorService es = Executors.newSingleThreadExecutor(); MyRunnable r = new MyRunnable(); Future<?> future = es.submit(r); // Sleep the current thread // Check the future System.out.println( "Future cancelled? " + future.isCancelled()); System.out.println( "Future done? " + future.isDone()); future.cancel( true ); // Check to make sure it really is canceled. System.out.println( "Future cancelled? " + future.isCancelled()); System.out.println( "Future done? " + future.isDone()); // Execute an orderly shutdown of our service. es.shutdown(); // Wait patiently for 10 seconds for the Threads to cleanup if (!es.awaitTermination( 10 , TimeUnit.SECONDS)) { // Kill the service regardless of Thread state. es.shutdownNow(); } } } |
Reference
- How do you cancel a scheduled Thread in an ExecutorService?
- How to exercise your Daemons - Callable, Future, and ExecutorService
- ExecutorService and CountDownLatch Example