Java 等待接口的异步实现

Java 等待接口的异步实现,java,asynchronous,Java,Asynchronous,目前,我必须使用服务的接口。该接口提供以下方法: public interface SomeServiceInterface{ // true - if the implementation of the interface is an asynchronous call, // false - if synchronous boolean isAsynchronous(); // a method, that can be implemented syn

目前,我必须使用服务的接口。该接口提供以下方法:

public interface SomeServiceInterface{    
   // true - if the implementation of the interface is an asynchronous call, 
   // false - if synchronous
    boolean isAsynchronous();

    // a method, that can be implemented synchronously and asynchrounously
    // it returns some response object
    ResponseObject someMethod();
}
还有一个简单的对象:

public class ResponseObject{
    private String foo;
    private int bar;
}
该接口的实现对我来说是隐藏的,我无法触摸它。现在我在我的类中有了一个方法,即获取类型为
SomeServiceInterface
的列表。对于同步操作,我想等待异步服务操作

我需要像这样的东西:

public oneOfMyImplementedMethod(){
// Getting a reference
List<SomeServiceInterface> serviceList = ...
   for(SomeServiceInterface service: serviceList){
      ResponseObject responseObject = null;
      if(service.isAsynchronous()){
         // !!! Here i want to wait until the asynchronous operation is finished, instead to continue
         responseObject = responseObject.someMethod();
      }
      else{
         responseObject = service.someMethod();
      }
      //... do something with the responseObject
   }
} 
public-oneOfMyImplementedMethod(){
//获得推荐人
列表服务列表=。。。
对于(SomeServiceInterface服务:serviceList){
ResponseObject ResponseObject=null;
if(service.isAsynchronous()){
//!!!这里我想等待异步操作完成,而不是继续
responseObject=responseObject.someMethod();
}
否则{
responseObject=service.someMethod();
}
//…对responseObject做点什么
}
} 
我怎样才能做到这一点?我不熟悉这个异步主题,我已经看到一些人在使用alrady。但我不能把它正确地转移到我的情况。有人能帮我吗?或者我必须用不同的东西

我很高兴你的帮助


非常感谢

您不能等到异步方法返回,因为它的本质是。它在幕后做所有的工作。为了知道工作何时完成,您需要一个回调方法。您可以注册一个事件处理程序,该事件处理程序将在作业完成时触发

public interface SomeServiceInterface{  
    boolean isAsynchronous();

    ResponseObject someMethod();

    void addEventHandler(MyProcessEventHandler h);
}
事件处理程序可以是这样的

public class MyProcessEventHandler{  

      public void onEvent(ProcessResult result){//Process result could be a class that holds the result of the process.
          //Do the work here. Here you process the result of the process call.
      }
}

您如何知道何时可以调用
responseObject.someMethod()
?理想情况下,异步操作应该有一个回调,在操作完成时调用该回调。根据你的界面,情况似乎并非如此。你有办法知道操作是否终止了吗?就像另一个人说的:你的接口不能满足你的要求。它要么允许回拨;或者回报某种未来或承诺。。。从某种意义上说:该接口不能让您理解异步调用何时完成。