Skip to content

Instantly share code, notes, and snippets.

@NikolayGorshkov
Created March 15, 2019 18:21
Show Gist options
  • Save NikolayGorshkov/da433be1916902c19b48f4c98eb9116c to your computer and use it in GitHub Desktop.
Save NikolayGorshkov/da433be1916902c19b48f4c98eb9116c to your computer and use it in GitHub Desktop.
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
public class ForkJoinTaskDemo {
public static void main(String[] args) throws Exception {
ForkJoinPool pool = ForkJoinPool.commonPool();
System.out.println("Pool size: " + pool.getParallelism());
ForkJoinTask<String> t1 = ForkJoinTask.adapt(() -> {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("T1 completed");
return "T1 result";
});
ForkJoinTask<String> t2 = ForkJoinTask.adapt(() -> {
String t1Result = t1.join();
System.out.println("T1 result: " + t1Result);
return "T2 result";
});
pool.submit(t1);
pool.submit(t2);
String t2Result = t2.join();
System.out.println("T2 result: " + t2Result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment