Ruby的ThreadGroup有什么用? [英] What is Ruby's ThreadGroup for?
问题描述
我正在浏览镐,在Thread
上查找文档,并遇到了ThreadGroup
.
I was flicking through the Pickaxe, looking for the documentation on Thread
, and came across ThreadGroup
.
文档描述了它的作用,但没有解释它的作用.
The documentation describes what it does, but it doesn't explain what it's for.
是与线程池相关的线程组,我假定Ruby没有?
Is a thread group related to a thread pool, which I assumed Ruby doesn't have?
推荐答案
新线程在其父级的ThreadGroup中创建.您可以使用ThreadGroup来组织由生成其他线程的父线程提供的隐式树结构,并使用list
实例方法来获取尚未终止的所有线程,即,定义在组中所有线程上运行的方法.
New threads are created in their parent's ThreadGroup. You can use the ThreadGroup to organize the implicit tree structure given by the parent threads spawning other threads, and use the list
instance method to get all threads which have not terminated yet, i.e. to define methods operating on all threads in the group.
此外,如果您运行不受信任的代码并希望关注其产生的线程,则可以使用enclose
禁止向该组添加(或删除)线程.
Additionaly, you can use enclose
to prohibit adding (or removing) threads to this group, if you run untrusted code and want to keep an eye on the threads it spawns.
这篇关于Ruby的ThreadGroup有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!