并行计算延迟
我正在使用parallel.for在多个线程中启动外部程序。但尽管事实上这些是单独的线程,我需要实现诸如延迟之类的东西。例如,2 个线程想要同时启动此外部程序 - 那么其中一个线程应该等待并在第二个线程后 10 秒启动。
是否可以?
I'm using parallel.for
to launch in many threads a external program. But despite the fact that these are separate threads I need implement sth like delay. E.g. 2 threads want to launch this external program at the same moment - then one of them should wait and start e.g. 10 sec after second thread.
Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是可能的,但考虑到您提供的信息,这似乎毫无意义......您正在强制外部程序的单线程执行,因此您也可能有一个线程执行它。如果
线程 2
必须等待线程 1
才能启动“外部程序”,那么就让线程 1
完成此后的所有工作它已经知道它何时启动“外部程序”。从多线程方法中获得的唯一好处是,如果在执行“外部程序”之前需要执行大量处理,并且该处理必须是并发执行的良好候选者。
更新
好的,有几种方法可以只用一个额外的线程来完成此操作,以保持主/GUI 线程的响应能力。第一种方法是围绕您正在交互的外部资源进行简单的锁定:
这里有 3 个用于执行代码的多线程版本:
Parallel.For
方法:如果外部程序需要,则推荐使用执行时间很短——我建议执行时间在 25 秒以内(尽管这不一定是“正确”的数字)。ThreadPool
:再次,我建议您执行少于 25 秒的操作(与上面的保留相同)。Thread
:如果操作运行时间较长(即超过 25 秒,但如果运行时间低于 25 秒,效果同样好),则建议使用该方法。以下是一些示例(不一定是功能性的,主要是为了让您了解不同的方法):
另一种选择是采用生产者/消费者设计模式,其中您的
ExternalResourceHandler
是消费者并进行处理从线程安全队列向外部资源发出请求。您的主线程只是将请求放入队列并立即返回工作。这是一个示例:您的主线程将如下所示:
如您所见:在这两种情况下,外部处理程序的所有工作都强制在单个线程上进行,但您的主线程仍然保持响应。
It's possible, but given the information you've provided it seems pointless... you're enforcing single-threaded execution of the external program, so you might as well have a single thread executing it. If
Thread 2
has to wait forThread 1
in order to start the "external program," then just letThread 1
do all the work since it already knows when it started the "external program."The only benefit you will get from a multi-threaded approach is if you have a bunch of processing that you need to do prior to executing the "external program" and that processing has to be a good candidate for concurrent execution.
Update
OK, there are a couple of ways to do this with only one extra thread in order to keep your Main/GUI thread responsive. The first approach is a simple lock around the external resource which you're interacting with:
Here are 3 multi-threaded version for executing the code:
Parallel.For
method: recommended if the external program takes a short amount of time to execute- I'd suggest for things under 25 seconds (although this is not necessarily a "correct" number).ThreadPool
: again, I'd recommend for things that take less than 25 seconds (with the same reservation as above).Thread
: this would be recommended if the operation runs longer (i.e. more than 25 seconds, but it would do just as good if it's under 25 seconds).Here are some examples (not necessarily functional, mostly meant to give you an idea of the different approaches):
The other option is to employ the Producer/Consumer design pattern where your
ExternalResourceHandler
is the consumer and it processes requests to the external resource from a thread-safe queue. Your main thread just places requests on the queue and immediately returns back to work. Here is an example:Your main would look like this:
As you see: in both cases all the work for the external handler is forced on a single thread yet your main thread still remains responsive.
看看生产者-消费者模式。第一个线程产生“外部程序已启动”信息,第二个线程使用该信息,等待 10 秒,然后启动外部程序。
Look at producer-consumer pattern. The first thread produces the information "external progam launched" the second thread consumes it, waits 10 second and then launches the external program.