fork(1) 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. }
  17. public Future<Integer> calculate(Integer input) {
  18. ExecutorService executor = Executors.newFixedThreadPool(1);
  19. return executor.submit(() -> {
  20. long start = System.currentTimeMillis();
  21. Thread.sleep(2000);
  22. System.out.println("Sleep time in ms = "+(System.currentTimeMillis()-start));
  23. return input * input;
  24. });
  25. }
  26. public Future<Integer> doCallable() {
  27. int value = 99;
  28. try {
  29. Callable<Future> callable = () -> calculate(value);
  30. Future<Integer> future = callable.call();
  31. return future;
  32. } catch (final Exception e) {
  33. e.printStackTrace();
  34. throw new RuntimeException(e);
  35. }
  36. }
  37. }
Time limit exceeded #stdin #stdout 5s 33684KB
stdin
Standard input is empty
stdout
Sleep time in ms = 2001
9801