fork download
  1. class Main {
  2.  
  3. static class Callee {
  4. public void call() { System.out.println("Method called"); }
  5.  
  6. public Callee() {
  7. try {Thread.sleep(1000);} catch (Exception e) {}
  8. System.out.println("I am constructed!");
  9. }
  10. }
  11.  
  12. static class Creator implements Runnable {
  13. public Callee callee = null;
  14.  
  15. public void run() { callee = new Callee(); }
  16. }
  17.  
  18. static class Caller implements Runnable {
  19. private Creator creator;
  20.  
  21. public Caller(Creator creator) { this.creator = creator; }
  22.  
  23. public void run() {
  24. while (creator.callee == null);
  25. creator.callee.call();
  26. }
  27. }
  28.  
  29. public static void main(String... args) {
  30. Creator creator = new Creator();
  31. new Thread(creator).start();
  32. new Thread(new Caller(creator)).start();
  33. }
  34. }
Success #stdin #stdout 1.03s 246272KB
stdin
Standard input is empty
stdout
I am constructed!
Method called