强制不同的线程休眠 [英] Force a Different Thread to Sleep

查看:152
本文介绍了强制不同的线程休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个程序,作为一种壳,用于其他项目。在它的核心,它传递了一个类,方法的名称,以及一些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?

推荐答案

不是真的。你可以尝试改变与<一个其他线程的优先级href="http://msdn.microsoft.com/en-us/library/system.threading.thread.priority.aspx"><$c$c>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.

有<一href="http://msdn.microsoft.com/en-us/library/system.threading.thread.suspend.aspx"><$c$c>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.

这篇关于强制不同的线程休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆