Reputation: 11
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);
this is my code wherein i want to invoke second and third thread only when first has completed processing. I am confused between AwaitTermination
and sleep
. Kindly suggest.
Upvotes: 0
Views: 81
Reputation: 5313
With Java 8 CompletableFuture you can write
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);
This will start the second and third tasks when the first completes.
Upvotes: 1
Reputation: 14829
Here's a simple possibility. There are other more complicated things that you could do, but if I understand your question correctly, this should do what you want:
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);
Upvotes: 0
Reputation: 853
If I understand your question correctly, using Guava's support for futures might be really helpful.
I would refer to this article by google to learn a little bit more about ListenableFuture
. Here is a code sample based on your code, though if you do plan to use Guava's Futures, I would probably build my code a bit differently:
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) {
}
});
What happens here is that first we submit the first worker, and then add 2 callbacks to it (secondWorker
and thirdWorker
). The onSuccess
method will be called once firstWorker
completes successfully. If it throws an exception, onFailure
will be called.
Upvotes: 0