Java 调用函数并在计时器中获取值

Java 调用函数并在计时器中获取值,java,Java,我有一个定义如下的函数: public Object test(int arg0){ final Time tt = new Timer(); tt.schedule(new TimerTask() { public void run(){ Object j = anotherMethod(arg0); //Do some checks on object j and if it fits, return t

我有一个定义如下的函数:

public Object test(int arg0){

     final Time tt = new Timer();
     tt.schedule(new TimerTask() {
         public void run(){
            Object j = anotherMethod(arg0);
            //Do some checks on object j and if it fits, return this j
            //I want to be able to return this object j that I get from another method
         }
     } , interval);
}

基本上,我想在TimerTask中的run方法中调用一个函数并进行一些检查,如果函数返回我想要的结果,我想在测试中返回它(run方法之上的方法)。我怎样才能做到这一点?

我认为使用TimerTask类的最好方法是让它扩展可观察的类并创建一个方法:

public void update(){
   setChanged();
   notifyObervers();
 }
您将调用方法内部的函数,该函数检测是否是执行某项操作的时间。包含检查某个内容的方法的类,它将注册为observer,并将实现接口observer的方法。你想做的事并不尊重坚实的范例。我希望这个答复对你有用