写FutureTask<Boolean>,多产品鉴权,用到了一个List
class="java" name="code">
List<FutureTask<Boolean>> batchAuthFutureTask = new ArrayList<FutureTask<Boolean>>();
for(int i=0;i<20;i++){
XXX x = new XXX(i);
FutureTask<Boolean> taskAuthOrder = new FutureTask<Boolean>(x);
ThreadPool.getCachedThreadPool().execute(taskAuthOrder);
batchAuthFutureTask.add(taskAuthOrder);
}
boolean auth = true;
for(FutureTask<Boolean> taskAuthOrder:batchAuthFutureTask){
auth = auth&&taskAuthOrder.get();
}
发现鉴权成功时(true)主
线程会等子线程跑完再跑,鉴权失败,主线程不等子线程全部跑完就会跑了,一开始没明白原因,甚至怀疑FutureTask<Boolean>是不是后台有这样的业务功能又觉得实在没道理,回看代码是&&前面为false,后面不执行。