fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.concurrent.Callable;
  6. import java.util.concurrent.ExecutionException;
  7. import java.util.concurrent.ExecutorService;
  8. import java.util.concurrent.Executors;
  9. import java.util.concurrent.Future;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. Ideone ideone = new Ideone();
  17.  
  18. ExecutorService executor = Executors.newFixedThreadPool(Runtime
  19. .getRuntime().availableProcessors());
  20. List<Callable<Boolean>> callables = new ArrayList<>();
  21. for (int counter = 0; counter < 8; counter++) {
  22. callables.add(ideone.new MyCallable());
  23. }
  24.  
  25. List<Future<Boolean>> futures = executor.invokeAll(callables);
  26. for (Future<Boolean> future : futures) {
  27. System.out.println(future.get());
  28. }
  29.  
  30. executor.shutdown();
  31.  
  32. }
  33.  
  34. private class MyCallable implements Callable<Boolean> {
  35.  
  36. @Override
  37. public Boolean call() {
  38. return Boolean.TRUE;
  39. }
  40. }
  41.  
  42. }
Success #stdin #stdout 0.13s 321856KB
stdin
Standard input is empty
stdout
true
true
true
true
true
true
true
true