单独线程中的无限循环 [英] infinite loops in separate threads

查看:299
本文介绍了单独线程中的无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些需要定期执行的数据库优化例程。我正在使用一个TTimer,但是主VCL冻结并且非常黑客...我想知道最好的方法是使用低CPU消耗并执行这些例程。我认为将例程放在具有低线程优先级的单独线程中是最好的方法。



任何想法?

解决方案

如果可能的话,只需编写所有的线程来完成在特定时间完成的最重要的事情。如果您不清楚您正在做什么,线程优先级可能导致严重的性能问题 。相反,只需对您的线程进行编码:


  1. 有什么重要吗?如果是这样做。


  2. 有什么不太重要吗?


  3. 转到步骤1。


说你使用线程优先级。想象一下:


  1. 一个低优先级的任务,A,抓住数据库的锁。


  2. 一个正常的优先级任务B需要大量的CPU时间,它会从低优先级的任务中窃取CPU。


  3. 正常优先级任务C需要访问数据库。但是它不能运行,因为低优先级任务保持对数据库的锁定,任务B使CPU超过任务A.


  4. 现在,任务C有等到任务B完成才能访问数据库。但是它应该与任务B相乘。



I have some data base optimization routines that need to execute periodically. I am currently using a TTimer but the main VCL freezes and is very hacky ... I would like to know what the best method is to have a low cpu consumption and execute these routines. I think putting the routines in separate threads with low thread priority is the best way.

Any ideas ?

解决方案

If possible, it is much better to just code all your threads to do the most important thing that needs to get done at that particular time. Messing with thread priorities can cause serious performance problems if you don't know exactly what you're doing. Instead, just code your threads like this:

  1. Is there anything important to do? If so do it.

  2. Is there anything not too important to do? If so, do a little of it.

  3. Go to step 1.

Say you do use thread priorities. Imagine this:

  1. A low priority task, A, grabs a lock on the database.

  2. A normal priority task, B, requires lots of CPU time, it steals the CPU from the low priority task.

  3. A normal priority task, C, requires access to the database. But it can't run because the low priority task holds the lock on the database and task B gets the CPU over task A.

  4. Now, task C has to wait until task B is complete to get access to the database. But it should be timeslicing with task B.

这篇关于单独线程中的无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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