Created
March 15, 2019 18:21
-
-
Save NikolayGorshkov/da433be1916902c19b48f4c98eb9116c to your computer and use it in GitHub Desktop.
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
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