简体   繁体   English

实现多个并发线程并在执行过程中返回结果的最佳方法?

[英]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. 每个线程将循环n次。 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 . 我目前正在使用CallableFuture ,并将所有线程返回的结果列表放入所有线程运行完毕并提供给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? Java并发库中是否有适合此目的的东西? 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. 您可以在结果出现时将结果传递给BlockingQueue。 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. java.util.concurrent.CyclicBarrier类是一种同步机制,可以同步通过某种算法进行的线程。 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 创建一个CyclicBarrier

When you create a CyclicBarrier you specify how many threads are to wait at it, before releasing them. 创建CyclicBarrier时,可以指定释放线程之前要等待的线程数。 Here is how you create a CyclicBarrier: 这是创建CyclicBarrier的方法:

CyclicBarrier barrier = new CyclicBarrier(2);

Waiting at a CyclicBarrier 在CyclicBarrier中等待

Here is how a thread waits at a CyclicBarrier: 这是线程在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. 超时后,即使不是所有的N个线程都在CyclicBarrier中等待,该线程也会被释放。 Here is how you specify a timeout: 这是您指定超时的方法:

barrier.await(10, TimeUnit.SECONDS);

The waiting threads waits at the CyclicBarrier until either: 等待线程在CyclicBarrier中等待,直到:

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. 最后一个线程到达(调用await())该线程被另一个线程中断(另一个线程调用其interrupt()方法)另一个等待线程被中断另一个等待线程在CyclicBarrier等待时超时CyclicBarrier.reset()方法是由某些外部线程调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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