带约束的并发任务执行的设计模式

发布于 2024-10-03 05:29:02 字数 723 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晨说晚安 2024-10-10 05:29:02

Objective-C Foundation 框架包含满足其中一些要求的类 NSOperationQueueNSOperationNSOperationQueue 表示一个 NSOperation 队列。队列同时运行可配置的最大数量的操作。操作具有优先级和一组依赖关系;操作所依赖的所有操作必须在队列开始运行该操作之前完成。这些操作被安排在动态大小的线程池上运行。

您需要的是一个更智能的 NSOperationQueue 版本,它应用您所表达的约束,但是 NSOperationQueue 和公司提供了一个示例,说明您的问题在生产中是如何大致解决的类似于您在线程池上运行任务的调度线程的第二个建议解决方案的框架。

The Objective-C Foundation framework includes classes NSOperationQueue and NSOperation that satisfy some of these requirements. NSOperationQueue represents a queue of NSOperations. 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, but NSOperationQueue 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.

噩梦成真你也成魔 2024-10-10 05:29:02

实际上,这比看起来更简单:主要需要一个互斥体:

IThread(int k) {
 synchronized (u_mutex) {
  if (previousUSet.contains(k))) U(k);
 }
 I(k);
}

DThread(int k) {
 synchronized (u_mutex) {
  D(k);
  previousUSet.add(k);
 }
}

Actually this turns out to be more simple than it seemed: a mutex is mainly all that is needed:

IThread(int k) {
 synchronized (u_mutex) {
  if (previousUSet.contains(k))) U(k);
 }
 I(k);
}

DThread(int k) {
 synchronized (u_mutex) {
  D(k);
  previousUSet.add(k);
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文