C# 降低线程池中线程的优先级

发布于 2024-08-08 04:50:47 字数 197 浏览 5 评论 0原文

当一些 cpu 时间可用时,我有几个低重要性的任务需要执行。如果其他更多导入任务正在运行,我不希望执行此任务。即,如果出现正常/高优先级任务,我希望低重要性任务暂停,直到重要任务完成。

有相当多的低重要性任务需要执行(50 到 1000 个)。所以我不想为每个任务创建一个线程。但是我相信线程池不允许某些优先级规范,不是吗?

你会如何解决这个问题?

I have several low-imprtance tasks to be performed when some cpu time is available. I don't want this task to perform if other more import task are running. Ie if a normal/high priority task comes I want the low-importance task to pause until the importance task is done.

There is a pretty big number of low importance task to be performed (50 to 1000). So I don't want to create one thread per task. However I believe that the threadpool do not allow some priority specification, does it ?

How would you do solve this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

我们只是彼此的过ke 2024-08-15 04:50:48

您不应该弄乱常规 ThreadPool 的优先级,因为您不是唯一的使用者。我认为逻辑方法是编写您自己的 - 也许就像生产者/消费者队列一样简单,使用您自己的线程作为消费者 - 自己设置线程优先级。

.NET 4.0 包含新的库(TPL 等)以使这一切变得更容易 - 在此之前您需要额外的代码来创建自定义线程池或工作队列。

You shouldn't mess with the priority of the regular ThreadPool, since you aren't the only consumer. I suppose the logical approach would be to write your own - perhaps as simple as a producer/consumer queue, using your own Thread(s) as the consumer(s) - setting the thread priority yourself.

.NET 4.0 includes new libraries (the TPL etc) to make all this easier - until then you need additional code to create a custom thread pool or work queue.

生生不灭 2024-08-15 04:50:48

当您使用 ThreadPool 中的构建时,所有线程都以默认优先级执行。如果你搞乱了这个设置,它将被忽略。在这种情况下,您应该推出自己的线程池。几年前,我扩展了 SmartThreadPool 来满足我的需求。这或许也能满足你的需求。

When you are using the build in ThreadPool all threads execute with the default priority. If you mess with this setting it will be ignored. This is a case where you should roll your own ThreadPool. A few years ago I extended the SmartThreadPool to meet my needs. This may satisfy yours as well.

风轻花落早 2024-08-15 04:50:48

我将创建一个待处理任务对象的共享队列,每个对象指定其优先级。然后编写一个调度程序线程来监视队列并为每个任务启动一个新线程,最多达到某个最大线程限制,并在创建线程时指定线程优先级。只需要做少量的工作,您就可以让调度程序报告活动,甚至动态调整正在运行的线程数。这个概念对我来说非常有效,如果您将队列设置为数据库表,则可以将其包装在 Windows 服务中进行启动。

I'd create a shared Queue of pending task objects, with each object specifying its priority. Then write a dispatcher thread that watches the Queue and launches a new thread for each task, up to some max thread limit, and specifying the thread priority as it creates it. Its only a small amount of work to do that, and you can have the dispatcher report activity and even dynamically adjust the number of running threads. That concept has worked very well for me, and can be wrapped in a windows service to boot if you make your queue a database table.

青衫负雪 2024-08-15 04:50:47

您可以新建一个线程并使用调度程序 发送各种优先级

优先级有点以 UI 为中心,但这并不重要。

You can new up a Thread and use a Dispatcher to send it takes of various priorities.

The priorities are a bit UI-centric but that doesn't really matter.

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