Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在离开方法之前等待回调(Java)_Java_Android - Fatal编程技术网

如何在离开方法之前等待回调(Java)

如何在离开方法之前等待回调(Java),java,android,Java,Android,我有一个方法,在其中调用另一个具有回调的方法。我想在离开我的方法之前收到这个回调。我看到了其他一些使用闩锁的柱子。我的代码如下所示: public void requestSecurityToken(<some params>){ final CountDownLatch latch = new CountDownLatch(1); MyFunction.execute(<someParams>, new RequestListener<Login

我有一个方法,在其中调用另一个具有回调的方法。我想在离开我的方法之前收到这个回调。我看到了其他一些使用闩锁的柱子。我的代码如下所示:

public  void requestSecurityToken(<some params>){
    final CountDownLatch latch = new CountDownLatch(1);
    MyFunction.execute(<someParams>, new RequestListener<Login>() {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            //TODO
        }

        @Override
        public void onRequestSuccess(Login login) {
            //handle some other stuff
            latch.countDown();

        }
    });


    try {
        latch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
public void requestSecurityToken(){
最终倒计时闩锁=新倒计时闩锁(1);
MyFunction.execute(,newrequestlistener(){
@凌驾
公共void onRequestFailure(SpiceException){
//待办事项
}
@凌驾
public void onRequestSuccess(登录){
//处理其他事情
倒计时();
}
});
试一试{
satch.wait();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
这不起作用,该方法被困在await()函数中。发生的情况是,该方法立即跳转到await(),而不再进入onRequestSuccess()或onRequestFailure()方法。我想这是一个一致性问题。。。关于如何解决这个问题有什么想法吗


编辑:在创建闩锁的位置添加代码行。

执行此操作时

newrequestlistener

您正在向实现接口的函数传递对象。
这就是为什么不调用这些方法的原因,只有当您获得请求结果(成功或失败)时才会调用这些方法。
你可以这样做

MyFunction.execute(<someParams>, new RequestListener<Login>() {
    @Override
    public void onRequestFailure(SpiceException spiceException) {
        someFunction();
    }

    @Override
    public void onRequestSuccess(Login login) {
        //handle some other stuff
        someFunction();
        latch.countDown();

    }
});





public void someFunction()[
   try {
    latch.await();
} catch (InterruptedException e) {
    e.printStackTrace();
}
  }
MyFunction.execute(,newrequestlistener(){
@凌驾
公共void onRequestFailure(SpiceException){
someFunction();
}
@凌驾
public void onRequestSuccess(登录){
//处理其他事情
someFunction();
倒计时();
}
});
公共函数()[
试一试{
satch.wait();
}捕捉(中断异常e){
e、 printStackTrace();
}
}

如果我错了,请纠正我的错误,但是在这种情况下,我的函数不会也被困在wait()中吗?(正如我们在倒计时之前所说的那样)