fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.concurrent.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. Ideone obj = new Ideone();
  14. Future<Integer> res = obj.doCallable();
  15. System.out.println(res.get());
  16. System.exit(0);
  17. }
  18. public Future<Integer> calculate(Integer input) {
  19. ExecutorService executor = Executors.newFixedThreadPool(1);
  20. return executor.submit(() -> {
  21. long start = System.currentTimeMillis();
  22. Thread.sleep(2000);
  23. System.out.println("Sleep time in ms = "+(System.currentTimeMillis()-start));
  24. return input * input;
  25. });
  26. }
  27. public Future<Integer> doCallable() {
  28. int value = 99;
  29. try {
  30. Callable<Future> callable = () -> calculate(value);
  31. Future<Integer> future = callable.call();
  32. return future;
  33. } catch (final Exception e) {
  34. e.printStackTrace();
  35. throw new RuntimeException(e);
  36. }
  37. }
  38. }
Success #stdin #stdout 0.21s 33888KB
stdin
Standard input is empty
stdout
Sleep time in ms = 2000
9801