Skip to content

Instantly share code, notes, and snippets.

@akaigoro
Created March 12, 2019 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akaigoro/734553139c87bee98e1ef190eb5c0536 to your computer and use it in GitHub Desktop.
Save akaigoro/734553139c87bee98e1ef190eb5c0536 to your computer and use it in GitHub Desktop.
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