我想做这样的事情:
public class MyCallable implements Callable<Boolean>, MyObserver
{
Boolean mSuccess = false;
@Override
public Boolean call() throws Exception
{
// ... Wait here until myCallback is called ...
return mSuccess;
}
@Override
public void myCallback()
{
if(something) mSuccess = true;
}
}
所以我可以像这样使用它:
MyCallable callable = new MyCallable();
FutureTask<Boolean> futureTask = new FutureTask<Boolean>(callable);
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.execute(futureTask);
Boolean successfull = futureTask.get();
这个“等待回调被调用”的代码是什么样的?我想我可以只创建一个名为“Boolean mCallbackCalled = false”的成员,然后在while(!mCallbackCalled),但这看起来很hacky......