how to make main thread wait for executor service threads to complete

Muddassir Rahman :

Here is my main thread , inside each process i am calling executor service threads. At line 4 , before updating the status , i want to wait for my all threads to complete

 line 1- missingStrategyContext.process(FileType.CARD, programId, fromDate, toDate, channel);
 line 2- missingStrategyContext.process(FileType.TRANSACTION, programId, fromDate, toDate, channel);
 line 3- missingStrategyContext.process(FileType.REFUND, programId, fromDate, toDate, channel);

 line 4- processingLog.setProcessingStatus(TransactionState.SUCCESS.name());

Inside each process i am deciding on file type value which strategy i have to call

public void process(FileType fileType, String programId, Long fromDate, Long toDate, String channel){
    map.get(fileType).process(programId, fromDate, toDate, channel, fileType);
}

and then depending on file type i have implemented my process method and called executor service in each file type implementation

@Override
public void process(String programId, Long fromDate, Long toDate, String channel, FileType fileType) {

    MultiTenantTxMgmt multiTenantTxMgmt = MultiTenantTxMgmtUtil.get(programId);
    EntityManager entityManager = null;

    List<MissingReason> reasonList = new ArrayList<>();
    try {
        entityManager = multiTenantTxMgmt.getEntityManager();
        reasonList = missingDataRepository.getDistinctMissingReasonForFileType(entityManager, fileType, TransactionState.READYTOATTEMPT);
    }catch (Exception exception) {
        logger.error(exception.getMessage(), exception);
        throw new UnkownBatchSaveException();
    } finally {
        entityManager.close();
    }

    reasonList.forEach(
            missingReason -> deExecutorService.dotask(() ->
                    missingTransactionStrategyContext.processMissingV3(missingReason, programId, fromDate, toDate, channel, fileType)
            )
    );

}
Aniket Sahrawat :

You can use CountDownLatch#await. Eg copied from docs:

CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(N);

for (int i = 0; i < N; ++i) // create and start threads
    new Thread(new Worker(startSignal, doneSignal)).start();

doSomethingElse();            // don't let run yet
startSignal.countDown();      // let all threads proceed
doSomethingElse();
doneSignal.await();           // wait for all to finish

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make the main wait for all threads to complete

How to make the main (calling) thread wait for a child thread to complete execution?

Make 1 Thread wait for an array of Threads to complete

Java Scheduled Executor Service wait all threads complete

make main thread wait for other threads to finish

Wait main thread until other threads not get complete!

How to make tkinter wait for a new thread to complete?

Make the main thread wait until all threads finish

Main thread wait for other threads

std::thread: How to wait (join) for any of the given threads to complete?

Java Thread Pools/Executor Service and wait()s - what happens to the threads & task queue?

Java: How to make this main thread wait for the new thread to terminate

How to wait for a number of threads to complete?

How to make destructor wait until other thread's job complete?

In c, how to make a thread wait for other threads to finish

How to limit number of threads created and wait main thread until any one thread finds answer?

Wait main thread until all the thread pools task complete of ExecutorService?

When task is submitted to threads in a thread pool via executor service how to ensure that only 1 thread can occupy a synchronized task at a time?

How to wait for RxPy parallel threads to complete

how to make thread2 wait for thread1 in c using posix-threads

How to force main thread to wait for all its child threads finish in Haskell

Wait for all threads in an Executor to finish?

will main thread exit before child threads complete execution?

How to wait for thread to complete without blocking UI

How to limit maximum number of threads created and wait main thread until any one thread finds answer, before exiting function?

Why does main thread wait for background threads to finish

Main thread to wait two parallel threads children java

How to wait for the function finish (Threads in Thread)

How to make thread wait in Python?