Class: ThreadGroup
Overview
ThreadGroup
provides a means of keeping track of a number of
threads as a group. A <code>Thread</code> can belong to only one
<code>ThreadGroup</code> at a time; adding a thread to a new group will
remove it from any previous group.
Newly created threads belong to the same group as the thread from which they
were created.
Constant Summary collapse
- Default =
thgroup_default
Instance Method Summary collapse
-
#add(thread) ⇒ Object
Adds the given thread to this group, removing it from any other group to which it may have previously belonged.
-
#enclose ⇒ Object
Prevents threads from being added to or removed from the receiving
ThreadGroup
. -
#enclosed? ⇒ Boolean
Returns
true
if thgrp is enclosed. -
#list ⇒ Array
Returns an array of all existing
Thread
objects that belong to this group.
Instance Method Details
#add(thread) ⇒ Object
Adds the given thread to this group, removing it from any other group to which it may have previously belonged.
puts "Initial group is #{ThreadGroup::Default.list}"
tg = ThreadGroup.new
t1 = Thread.new { sleep }
t2 = Thread.new { sleep }
puts "t1 is #{t1}"
puts "t2 is #{t2}"
tg.add(t1)
puts "Initial group now #{ThreadGroup::Default.list}"
puts "tg group now #{tg.list}"
produces:
Initial group is #<Thread:0x401bdf4c>
t1 is #<Thread:0x401b3c90>
t2 is #<Thread:0x401b3c18>
Initial group now #<Thread:0x401b3c18>#<Thread:0x401bdf4c>
tg group now #<Thread:0x401b3c90>
13243 13244 13245 |
# File 'eval.c', line 13243 static VALUE thgroup_add(group, thread) VALUE group, thread; |
#enclose ⇒ Object
Prevents threads from being added to or removed from the receiving ThreadGroup
. New threads can still be started in an enclosed ThreadGroup
.
ThreadGroup::Default.enclose #=> #<ThreadGroup:0x4029d914>
thr = Thread::new { Thread.stop } #=> #<Thread:0x402a7210 sleep>
tg = ThreadGroup::new #=> #<ThreadGroup:0x402752d4>
tg.add thr
produces:
ThreadError: can't move from the enclosed thread group
13184 13185 13186 |
# File 'eval.c', line 13184 static VALUE thgroup_enclose(group) VALUE group; |
#enclosed? ⇒ Boolean
Returns true
if thgrp is enclosed. See also ThreadGroup#enclose.
13205 13206 13207 |
# File 'eval.c', line 13205 static VALUE thgroup_enclosed_p(group) VALUE group; |
#list ⇒ Array
Returns an array of all existing Thread
objects that belong to this group.
ThreadGroup::Default.list #=> [#<Thread:0x401bdf4c run>]
13144 13145 13146 |
# File 'eval.c', line 13144 static VALUE thgroup_list(group) VALUE group; |