强制不同的线程休眠

编程入门 行业动态 更新时间:2024-10-24 00:24:30
本文介绍了强制不同的线程休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以,我有一个程序,作为一种壳,用于其他项目。在它的核心,它传递了一个类,方法的名称,以及一些ARGS,并处理功能的执行,以允许其他程序员基本上安排他们的过程,在这个shell中运行的服务的理念。一切正常,除了一个问题。通常情况下,计划执行,这些过程都非常的CPU沉重。有时,叫流程开始使用如此多的CPU,那我有哪些是负责检查的时间表,并发射了其他作业,不会得到运行了相当一段时间的机会,从而导致调度问题和一个线程缺乏足够的响应。不幸的是,我不能插入Thread.sleep()方法,在实际运行code调用,因为我真的不拥有它。

So I have a program that serves as a sort of 'shell' for other programs. At its core, it gets passed a class, a method name, and some args, and handles the execution of the function, with the idea of allowing other programmers to basically schedule their processes to run on this shell service. Everything works fine except for one issue. Frequently, these processes that are scheduled for execution are very CPU heavy. At times, the processes called start using so much of the CPU, that the threads that I have which are responsible for checking schedules and firing off other jobs don't get a chance to run for quite some time, resulting in scheduling issues and a lack of sufficient responsiveness. Unfortunately, I can't insert Thread.Sleep() calls in the actual running code, as I don't really 'own' it.

所以我的问题是:是否有可能强制任意线程(我开始),睡觉(收益率)每隔一段时间,无需修改实际code运行在该线程?除了上述,是有一些方法来'注入'Thread.sleep()方法调用到code,我都快要动态地运行,在运行时?

So my question is: Is it possible to force an arbitrary thread (that I started) to sleep (yield) every so often, without modifying the actual code running in that thread? Barring that, is there some way to 'inject' Thread.Sleep() calls into code that I'm about to run dynamically, at run-time?

推荐答案

不是真的。你可以尝试改变与Priority属性,但你真的不能注入一个睡眠的呼叫。

Not really. You could try changing the priority of the other thread with the Priority property, but you can't really inject a "sleep" call.

有Thread.Suspend()但我的强烈的建议你不要使用它。你真的不的需要的拿到另一个线程暂停在任意时间,因为它可能拥有的重要资源。 (这就是为什么该方法被标记为过时,拥有各类警告周围:)

There's Thread.Suspend() but I strongly recommend you don't use it. You don't really want to get another thread to suspend at arbitrary times, as it might hold important resources. (That's why the method is marked as obsolete and has all kinds of warnings around it :)

减少其他任务的优先级,并可能提高你自己的任务的优先级可能是最好的办法。

Reducing the priority of the other tasks and potentially boosting the priority of your own tasks is probably the best approach.

更多推荐

强制不同的线程休眠

本文发布于:2023-10-24 17:16:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524531.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!