ExecutorService executor = Executors.newFixedThreadPool(10);
Runnable firstWorker = new DataComparison(DataComparison.FIRST_THREAD_INSTANCE,args[0], args[1], args[2], runSkey, fic_mis_date,legal_entity, args[6],failover);
executor.execute(firstWorker);
Runnable secondWorker = new DataComparison(DataComparison.SECOND_THREAD_INSTANCE,args[0], args[1], args[2], runSkey, fic_mis_date,legal_entity, args[6],failover);
executor.execute(secondWorker);
Runnable thirdWorker = new DataComparison(DataComparison.THIRD_THREAD_INSTANCE,args[0], args[1], args[2], runSkey, fic_mis_date,legal_entity, args[6],failover);
executor.execute(thirdWorker);
这是我的代码,其中我想只在第一个线程完成处理时调用第二个和第三个线程。我分不清AwaitTermination
和sleep
。请建议。
使用Java 8 CompletableFuture你可以写
ExecutorService executor = Executors.newFixedThreadPool(10);
Runnable firstWorker = new DataComparison(DataComparison.FIRST_THREAD_INSTANCE,args[0], args[1], args[2], runSkey, fic_mis_date,legal_entity, args[6],failover);
Runnable secondWorker = new DataComparison(DataComparison.SECOND_THREAD_INSTANCE,args[0], args[1], args[2], runSkey, fic_mis_date,legal_entity, args[6],failover);
Runnable thirdWorker = new DataComparison(DataComparison.THIRD_THREAD_INSTANCE,args[0], args[1], args[2], runSkey, fic_mis_date,legal_entity, args[6],failover);
CompletableFuture firstTask = CompletableFuture.runAsync(firstWorker,
executor);
firstTask.thenRunAsync(secondWorker,executor)
firstTask.thenRunAsync(thirdWorker,executor);
这将在第一个任务完成时启动第二个和第三个任务。
如果我对你的问题理解正确的话,使用Guava对期货的支持可能真的很有帮助。我会参考谷歌的这篇文章来学习更多关于ListenableFuture
的知识。下面是基于您的代码的代码示例,但是如果您确实计划使用Guava的Futures,我可能会以稍微不同的方式构建代码:
ListeningExecutorService s = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
Runnable firstWorker = new DataComparison(1);
final Runnable secondWorker = new DataComparison(2);
final Runnable thirdWorker = new DataComparison(3);
ListenableFuture f = s.submit(firstWorker);
Futures.addCallback(f, new FutureCallback() {
@Override
public void onSuccess(Object o) {
secondWorker.run();
}
@Override
public void onFailure(Throwable throwable) {
}
});
Futures.addCallback(f, new FutureCallback() {
@Override
public void onSuccess(Object o) {
thirdWorker.run();
}
@Override
public void onFailure(Throwable throwable) {
}
});
这里发生的事情是,我们首先提交第一个worker,然后向它添加2个回调(secondWorker
和thirdWorker
)。一旦firstWorker
成功完成,就会调用onSuccess
方法。如果抛出异常,则调用onFailure
。
这是一个简单的可能性。你还可以做其他更复杂的事情,但如果我正确理解了你的问题,这应该是你想要的:
final Runnable firstWorker = new DataComparison(DataComparison.FIRST_THREAD_INSTANCE,args[0], args[1], args[2], runSkey, fic_mis_date,legal_entity, args[6],failover);
final Runnable secondWorker = new DataComparison(DataComparison.SECOND_THREAD_INSTANCE,args[0], args[1], args[2], runSkey, fic_mis_date,legal_entity, args[6],failover);
final Runnable thirdWorker = new DataComparison(DataComparison.THIRD_THREAD_INSTANCE,args[0], args[1], args[2], runSkey, fic_mis_date,legal_entity, args[6],failover);
Runnable combined = new Runnable() {
@Override
public void run() {
firstWorker.run();
executor.execute(secondWorker);
executor.execute(thirdWorker);
}
};
executor.execute(combined);