带约束的并发任务执行的设计模式
我有 3 类任务(I、D、U),它们进入队列,同一类的任务必须按顺序处理。我希望任务尽可能同时运行;但是有一些限制:
- U 和 D 不能同时运行
- U 和 I 不能同时运行
- I(n) 要求 U(n) 已完成
Q: 哪种设计模式适合此类问题?
我正在考虑两种方法:
方法 1: 每个任务使用 1 个线程,每个线程都有自己的队列。每个线程都有一个同步启动阶段,在该阶段检查启动条件,然后运行,然后是同步停止阶段。很容易看出这将提供良好的并发性,但我不确定它是否正确实现我的约束并且不会死锁。
D_Thread { ...
while (task = D_Queue.take()) {
synchronized (State) { // start phase
waitForU();
State.setRunning(D, true);
}
run(task); // run phase
synchronized (State) { // stop phase
State.setRunning(D, false)
}
}
}
方法 2: 或者,单个调度线程管理执行状态,并在 ThreadPool 中调度任务,必要时等待当前调度的任务完成。
I have 3 classes of task (I, D, U) which come in on a queue, tasks of the same class must be processed in order. I want tasks to run as concurrently as possible; however there are some constraints:
- U and D cannot run concurrently
- U and I cannot run concurrently
- I(n) requires U(n) has completed
Q: What design pattern(s) would fit this class of problem?
I have two approaches I am considering:
Approach 1:
Use 1 Thread per task, each with its own queue. Each thread has a synchronized start phase where it checks start conditions, then runs, then a synchronized stop phase. It is easy to see that this will provide good concurrency but I am unsure if it correctly implements my constraints and doesnt deadlock.
D_Thread { ...
while (task = D_Queue.take()) {
synchronized (State) { // start phase
waitForU();
State.setRunning(D, true);
}
run(task); // run phase
synchronized (State) { // stop phase
State.setRunning(D, false)
}
}
}
Approach 2: Alternatively, a single dispatch thread manages execution state, and schedules tasks in a ThreadPool, waiting if necessary for currently scheduled tasks to complete.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Objective-C Foundation 框架包含满足其中一些要求的类
NSOperationQueue
和NSOperation
。NSOperationQueue
表示一个NSOperation
队列。队列同时运行可配置的最大数量的操作。操作具有优先级和一组依赖关系;操作所依赖的所有操作必须在队列开始运行该操作之前完成。这些操作被安排在动态大小的线程池上运行。您需要的是一个更智能的
NSOperationQueue
版本,它应用您所表达的约束,但是NSOperationQueue
和公司提供了一个示例,说明您的问题在生产中是如何大致解决的类似于您在线程池上运行任务的调度线程的第二个建议解决方案的框架。The Objective-C Foundation framework includes classes
NSOperationQueue
andNSOperation
that satisfy some of these requirements.NSOperationQueue
represents a queue ofNSOperation
s. The queue runs a configurable maximum number of operations concurrently. Operations have a priority and a set of dependencies; all of the operations that an operation depends on must be completed before the queue will start running the operation. The operations are scheduled to run on a dynamically-sized pool of threads.What you need requires a somewhat smarter version of
NSOperationQueue
that applies the constraints you have expressed, butNSOperationQueue
and company provide an example of how roughly your problem has been solved in a production framework that resembles your second suggested solution of a dispatch thread running tasks on a thread pool.实际上,这比看起来更简单:主要需要一个互斥体:
Actually this turns out to be more simple than it seemed: a mutex is mainly all that is needed: