Java CountDownLatch完成异步回调实例详解
JavaCountDownLatch完成异步回调实例详解
实例代码:
publicclassAsyncDemo{
privatestaticvoiddoSomeTask(){
System.out.println("HelloWorld");
}
privatestaticvoidonCompletion(){
System.out.println("Alltasksfinished");
}
publicstaticvoidmain(String[]args){
ExecutorServiceexecutor=Executors.newCachedThreadPool();
finalCountDownLatchlatch=newCountDownLatch(2);
executor.execute(newTask(latch));
executor.execute(newTask(latch));
executor.execute(()->{
try{
latch.await();
}catch(InterruptedExceptione){
e.printStackTrace();
}
onCompletion();
});
executor.shutdown();
}
privatestaticclassTaskimplementsRunnable{
/**
*CountDownLatch是JDK提供的一个简单的线程监测工具
*基于简单的计数,调用countDown()方法表明当前线程已经终止
*在监测线程中调用await()方法,该方法会一直挂起直到所有其它线程终止
*/
privatefinalCountDownLatchlatch;
publicTask(CountDownLatchlatch){
this.latch=latch;
}
@Override
publicvoidrun(){
try{
doSomeTask();
}catch(Exceptione){
e.printStackTrace();
}finally{
latch.countDown();
}
}
}
}
这里有两点需要补充:
1.如果你是用main方法启动的线程,这种调用方法是没有问题的,JDK会确保所有线程都终止以后main方法才退出。但是如果main方法不是异步任务的启动者(如JUnit,Spring,Tomcat),一旦启动之后laucher将会失去对线程的控制。如在JUnit中laucher提交完任务后就会被认为所有过程已完成,其它线程会被强行终止。
2.正因为如此,请根据环境使用正确的Executor。比如,在web环境中,应该选用tomcat(或Spring)管理的线程池作为Executor,这样才能确保web应用对于异步任务的整个生命周期具有控制权;如果你选用JDK的线程池有什么后果呢?任务也许可以正常执行,当一旦你终止web-app,正在执行的异步线程并不会被正常kill掉,并由此造成内存泄漏或其它不可预见的后果。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
