Delphi:线程作业的线程列表 - 排队 [英] Delphi: Threaded list of thread jobs - queueing

查看:271
本文介绍了Delphi:线程作业的线程列表 - 排队的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些基于TThreads的操作。现在我需要创建一个包含要完成的作业列表的线程,然后在上一个完成之后激活每个作业...我该怎么写?我不能允许线程同时运行,因为可能需要执行超过10000个操作。
很难找到TEvent和其他同步对象的文档示例...
希望我会在这里找到一些帮助...



提前感谢
michal

解决方案

不要将您的操作作为线程。这是错误的设计。相反,您应该为您的操作创建一个基类,该公开了一种执行操作的方法。写下后代课来实施具体的操作。不要对线程上下文进行任何假设,而是使用关键部分或类似的同步对象来保护对共享资源的访问。更重要的是,尽量避免共享资源,或至少尝试使共享资源为只读,以便锁定是不必要的。



可以通过直接调用操作方法来执行VCL线程中的每个操作,使用 TThread descendant类在自己的线程中执行操作(您似乎拥有现在),或者调度线程池上的所有操作。可以在运行时调整池中的线程数,以匹配操作的性质(处理器绑定或I / O绑定)和系统具有的处理器核心数。并回答您的问题:甚至可以通过强制池使用单个线程来完全序列化操作。基本上,您可以完全改变您的操作方式,而无需更改他们


I have some operations which are based on TThreads. Now I need to create the thread containing the list of jobs to be done, then firing each one as soon as the previous finishes... How should I write it? I can't allow the threads to be ran simultaneously as there might be over 10 000 operations to be done. It is quite hard to find documented examples of TEvent and other syncing objects... Hope I'll find some help here ...

Thanks in advance, michal

解决方案

Don't base your operations on threads. This is the wrong design. Instead you should create a base class for your operation, which exposes a method to perform the operation. Write descendant classes to implement the concrete operations. Don't make any assumptions about thread contexts, alway use critical sections or similar synchronization objects to protect access to shared resources. More importantly, try to avoid shared resources, or at least try to make shared resources read-only, so that locking isn't necessary.

With that design in place it becomes possible to perform each operation in the VCL thread by calling the operation method directly, to use a TThread descendant class to perform an operation in its own thread (what you seem to have now), or to schedule all operations on a thread pool. The number of threads in the pool can be adjusted at runtime to match the nature of the operations (processor-bound or I/O-bound) and the number of processor cores the system has. And to answer your question: it is even possible to completely serialize the operations by forcing the pool to use a single thread. Basically you can completely change the way your operations are performed without changing them at all.

这篇关于Delphi:线程作业的线程列表 - 排队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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