fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.concurrent.Callable;
  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 Exception
  12. {
  13. RunAndCall<RunnableAndCallable> instance = new RunAndCall<>();
  14. instance.invoke(new RunnableAndCallable());
  15. }
  16.  
  17. public static class RunAndCall<T extends Runnable & Callable<Integer>> {
  18.  
  19. public void invoke(T input) throws Exception {
  20. input.run();
  21. Integer result = input.call();
  22. System.out.println(result);
  23. }
  24. }
  25.  
  26. public static class RunnableAndCallable implements Runnable, Callable<Integer> {
  27.  
  28. public void run() {
  29. System.out.println("I can be run!");
  30. }
  31.  
  32. public Integer call() {
  33. return 12;
  34. }
  35. }
  36. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
I can be run!
12