Created
March 12, 2019 16:47
-
-
Save akaigoro/734553139c87bee98e1ef190eb5c0536 to your computer and use it in GitHub Desktop.
Illustration to the post http://jsr166-concurrency.10961.n7.nabble.com/proposal-Thread-currentExecutor-td15125.html
This file contains 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
public class CurrentExec { | |
public static Executor currentExecutor() { | |
Thread currentThread = Thread.currentThread(); | |
if (currentThread instanceof ForkJoinWorkerThread) { | |
return ((ForkJoinWorkerThread) currentThread).getPool(); | |
} | |
ThreadGroup group = currentThread.getThreadGroup(); | |
if (group instanceof Executor) { | |
return (Executor)group; | |
} | |
return null; | |
} | |
static String asyncJob0() { | |
return "asyncJob0"; | |
} | |
@Test | |
public void test0() throws ExecutionException, InterruptedException { | |
Executor exec = ForkJoinPool.commonPool(); | |
CompletableFuture<String> cf0 = CompletableFuture.supplyAsync(CurrentExec::asyncJob0, exec); | |
System.out.println(cf0.get()); | |
} | |
static String asyncJob1() { | |
Executor exec = currentExecutor(); | |
CompletableFuture<String> cf0 = CompletableFuture.supplyAsync(CurrentExec::asyncJob0, exec); | |
String part1 = "+asyncJob1"; | |
try { | |
return cf0.get()+part1; | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
@Test | |
public void test1() throws ExecutionException, InterruptedException { | |
Executor exec = ForkJoinPool.commonPool(); | |
CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(CurrentExec::asyncJob1, exec); | |
System.out.println(cf1.get()); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment