fork download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.concurrent.Callable;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.Executors;
  6. import java.util.concurrent.Future;
  7.  
  8. public class Main {
  9.  
  10. private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();
  11.  
  12. private static Callable<String> STAGE_1 = new Callable<String>() {
  13. @Override
  14. public String call() throws Exception {
  15. Thread.sleep(1000);
  16. return "Step1";
  17. }
  18. };
  19.  
  20. private static class Stage2 implements Callable<String> {
  21. private final Future<String> step1;
  22. private final int subType;
  23.  
  24. public Stage2(Future<String> step1, int subType) {
  25. this.step1 = step1;
  26. this.subType = subType;
  27. }
  28.  
  29. @Override
  30. public String call() throws Exception {
  31. return step1.get() + " " + subType;
  32. }
  33. }
  34.  
  35.  
  36. public static void main(String[] args) throws Exception {
  37. Future<String> step1Result = EXECUTOR.submit(STAGE_1);
  38. List<Stage2> list = new ArrayList<Stage2>(5);
  39. for (int i = 0; i < 5; i++) {
  40. list.add(new Stage2(step1Result, i));
  41. }
  42. List<Future<String>> step2Results = EXECUTOR.invokeAll(list);
  43. for (Future<String> result : step2Results) {
  44. System.out.println(result.get());
  45. }
  46. }
  47. }
  48.  
Time limit exceeded #stdin #stdout 5s 382144KB
stdin
Standard input is empty
stdout
Step1 0
Step1 1
Step1 2
Step1 3
Step1 4