Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import java.util.concurrent.ForkJoinPool;
  2. import java.util.concurrent.ForkJoinTask;
  3.  
  4. public class ForkJoinTaskDemo {
  5.  
  6. public static void main(String[] args) throws Exception {
  7.  
  8. ForkJoinPool pool = ForkJoinPool.commonPool();
  9. System.out.println("Pool size: " + pool.getParallelism());
  10.  
  11. ForkJoinTask<String> t1 = ForkJoinTask.adapt(() -> {
  12. try {
  13. Thread.sleep(10000);
  14. } catch (InterruptedException e) {
  15. e.printStackTrace();
  16. }
  17. System.out.println("T1 completed");
  18. return "T1 result";
  19. });
  20. ForkJoinTask<String> t2 = ForkJoinTask.adapt(() -> {
  21. String t1Result = t1.join();
  22. System.out.println("T1 result: " + t1Result);
  23. return "T2 result";
  24. });
  25.  
  26. pool.submit(t1);
  27. pool.submit(t2);
  28.  
  29. String t2Result = t2.join();
  30.  
  31. System.out.println("T2 result: " + t2Result);
  32.  
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement