Last active
December 14, 2015 12:39
-
-
Save nelsnelson/5088126 to your computer and use it in GitHub Desktop.
Test demo to show how future.cancel(true); does not seem to be working. test/InterruptDemo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.lang.InterruptedException; | |
import java.lang.Runnable; | |
import java.util.Calendar; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.Executor; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.Future; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.TimeUnit; | |
/* | |
Current time: 10:55:29 PM | |
Scheduled event for 10 seconds from now | |
f1: Current time: 10:55:29 PM | |
f1: Current time: 10:55:39 PM | |
f1: Current time: 10:55:39 PM | |
Cancelling f1 | |
f2: Current time: 10:55:39 PM | |
Got f1: Current time: 10:55:39 PM | |
Got f2: Current time: 10:55:39 PM | |
Sleeping to let other tasks finish before exiting | |
f1: Current time: 10:55:49 PM | |
Done sleeping | |
*/ | |
public class InterruptDemo { | |
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); | |
private final ExecutorService processor = Executors.newCachedThreadPool(); | |
@SuppressWarnings("unchecked") | |
public Future delay_event(Integer delay, Callable c) { | |
return scheduler.schedule((Callable<Object>) c, delay * 1000, TimeUnit.MILLISECONDS); | |
} | |
@SuppressWarnings("unchecked") | |
public Future execute(Callable c) { | |
return processor.submit((Callable<Object>) c); | |
} | |
public static class Boilerplate0 implements Callable { | |
public Object call() { | |
System.out.format("f0: Current time: %tr\n", Calendar.getInstance()); | |
return (Object) null; | |
} | |
} | |
public static class Boilerplate1 implements Callable { | |
public Object call() { | |
try { | |
System.out.format("f1: Current time: %tr\n", Calendar.getInstance()); | |
Thread.sleep(30000); | |
System.out.format("f1: Current time: %tr\n", Calendar.getInstance()); | |
} catch (InterruptedException ex) { | |
System.out.format("Interrupted future f1 %tr\n", Calendar.getInstance()); | |
} | |
return (Object) null; | |
} | |
} | |
public static class Canceller implements Callable { | |
private Future future = null; | |
public Canceller(Future future) { | |
this.future = future; | |
} | |
public Object call() { | |
System.out.println("Cancelling f1"); | |
System.out.format("f2: Current time: %tr\n", Calendar.getInstance()); | |
future.cancel(true); | |
return (Object) null; | |
} | |
} | |
public static void main(String[] argv) { | |
int n = 10; | |
if (argv.length > 0) { | |
String arg = argv[0]; | |
if (arg != null && arg.length() > 0) { | |
n = Integer.parseInt(arg); | |
} | |
} | |
new InterruptDemo(n); | |
} | |
public InterruptDemo(Integer n) { | |
Future f0 = delay_event(n, new Boilerplate0()); | |
System.out.format("Current time: %tr\n", Calendar.getInstance()); | |
System.out.println("Scheduled event for " + n + " seconds from now"); | |
Future f1 = execute(new Boilerplate1()); | |
Future f2 = delay_event((n / 2), new Canceller(f1)); | |
try { | |
f1.get(); | |
} catch (InterruptedException ex) { | |
System.out.println("Interrupted f1: " + ex); | |
} catch (Throwable t) { | |
System.out.println("WTF f1: " + t.getMessage()); | |
} | |
System.out.format("Got f1: Current time: %tr\n", Calendar.getInstance()); | |
try { | |
f2.get(); | |
} catch (InterruptedException ex) { | |
System.out.println("Interrupted f2: " + ex); | |
} catch (Throwable t) { | |
System.out.println("WTF f2: " + t.getMessage()); | |
} | |
System.out.format("Got f2: Current time: %tr\n", Calendar.getInstance()); | |
System.out.println("Sleeping to let other tasks finish before exiting"); | |
try { | |
Thread.sleep((n * 4) * 1000); | |
} catch (InterruptedException ex) { | |
System.out.println("Interrupted sleeping"); | |
} | |
System.out.println("Done sleeping for " + (n * 4) + " seconds"); | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment