如何在Java中创建异步方法的同步版本?
假设您有一个包含以下两个方法的类:
asyncDoSomething(); // Starts an asynchronous task
onFinishDoSomething(); // Called when the task is finished
如何实现一个同步的doSomething()
方法,在任务完成之前不返回任何结果?
如何在Java中创建异步方法的同步版本?
假设您有一个包含以下两个方法的类:
asyncDoSomething(); // Starts an asynchronous task
onFinishDoSomething(); // Called when the task is finished
如何实现一个同步的doSomething()
方法,在任务完成之前不返回任何结果?
看一下CountDownLatch。你可以用类似这样的方式模拟所需的同步行为:
private CountDownLatch doneSignal = new CountDownLatch(1);
void main() throws InterruptedException{
asyncDoSomething();
//wait until doneSignal.countDown() is called
doneSignal.await();
}
void onFinishDoSomething(){
//do something ...
//then signal the end of work
doneSignal.countDown();
}
您也可以使用带有2个参与者的CyclicBarrier
来实现相同的行为,如下所示:
private CyclicBarrier barrier = new CyclicBarrier(2);
void main() throws InterruptedException{
asyncDoSomething();
//wait until other party calls barrier.await()
barrier.await();
}
void onFinishDoSomething() throws InterruptedException{
//do something ...
//then signal the end of work
barrier.await();
}
如果你有 asyncDoSomething()
的源代码控制权,我建议将其重新设计为返回一个 Future<Void>
对象。这样做可以在需要时轻松切换异步/同步行为,如下所示:void asynchronousMain(){
asyncDoSomethig(); //ignore the return result
}
void synchronousMain() throws Exception{
Future<Void> f = asyncDoSomething();
//wait synchronously for result
f.get();
}