简体   繁体   中英

Best way to implement multiple concurrent threads that return results in the middle of execution?

I am implementing the following functionality in a load test tool to simulate heavy load on a target application:

Multiple threads are launched in concurrency to perform the same kind of operations.

Each thread will loop for n times. At the end of each loop, test results are available and are added to a list which is returned after all loops finish running.

I'm currently using Callable and Future , and putting lists of results returned by all the threads into another list after all threads finish running and give me the Future . The problem is that I can lose what is available if the execution of the program is interrupted. I want to be able to save results that are available in finishes loops while the threads are still processing remaining loops.

Is there something in Java concurrency library suitable for this purpose? Or is there a better design to the load test functionality I am building?

Thanks in advance!

You can pass your results to a BlockingQueue as they occur. This can be picked up by another thread or the one which triggered the tasks in the first place.

The java.util.concurrent.CyclicBarrier class is a synchronization mechanism that can synchronize threads progressing through some algorithm. In other words, it is a barrier that all threads must wait at, until all threads reach it, before any of the threads can continue.

Creating a CyclicBarrier

When you create a CyclicBarrier you specify how many threads are to wait at it, before releasing them. Here is how you create a CyclicBarrier:

CyclicBarrier barrier = new CyclicBarrier(2);

Waiting at a CyclicBarrier

Here is how a thread waits at a CyclicBarrier:

barrier.await();

You can also specify a timeout for the waiting thread. When the timeout has passed the thread is also released, even if not all N threads are waiting at the CyclicBarrier. Here is how you specify a timeout:

barrier.await(10, TimeUnit.SECONDS);

The waiting threads waits at the CyclicBarrier until either:

The last thread arrives (calls await() ) The thread is interrupted by another thread (another thread calls its interrupt() method) Another waiting thread is interrupted Another waiting thread times out while waiting at the CyclicBarrier The CyclicBarrier.reset() method is called by some external thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM