如何添加方法调用者的引用?

How to add reference of the caller of the method?

好的,我想不出更好的标题来描述我的问题。

我有一个计时器线程。它滴答作响,经过足够多的滴答声,我希望它调用调用它的方法。如果有人曾经使用过 Unity,我正在尝试这样做:

public void test(String CallMeLater){
   Invoke(CallMeLater);  // Unity
   caller.CallMeLater(); // Idea
}

值得注意的是调用它的东西不是静态的。 我该怎么做?

这是我的资料:

时钟

public class Clock extends Thread {
   Object test;

   public Clock(int minutes, Object test) {
      this.minutes = minutes;
      this.test = test;
   }

   public void run() {
      try {
         Thread.sleep(1000);
      } catch(Exception err) {}

      test.SayHi();
      run();
   }
}

来电者

public class MainWindow {
   // When the Clock is created i want to pass "this" into it.
   app.Clock clock = new app.Clock(60, this); 

   public MainWindow(){
      // Creates frame, and add listeners. I cut it out. 
      // It has nothing to do with the problem at hand
   }

   private void Set(){
      // This is where i start the timer
      clock.start();  
   }

   public void SayHi(){
      System.out.println("Hi");
   }
}

回调是接口的一个很好的用例。

public Inteface Callback {
    public void complete();
}

public class Ticker {
    public void test(Callback callback) {
        // tick, tick, tick
        callback.complete();
    }
}

public class User implements Callback {
    public void complete() {
        ...
    }

    public void useTicker() {
        Ticker ticker = new Ticker();
        ticker.test(this);
    }
}

请注意,Java 有许多看起来与此完全相同的内置界面。事实上,定时器 class 有一个很好的例子:TimerTask,它正是你在这里寻找的东西。