種類
- インスタンスメソッド (324)
- 特異メソッド (126)
キーワード
- [] (11)
-
abort
_ on _ exception (11) -
abort
_ on _ exception= (22) -
add
_ trace _ func (11) - alive? (11)
- backtrace (11)
-
backtrace
_ locations (22) - exit (11)
- fetch (7)
- fork (11)
-
handle
_ interrupt (11) -
ignore
_ deadlock= (3) - inspect (11)
- join (22)
- kill (22)
- list (11)
- name= (9)
- pass (11)
-
pending
_ interrupt? (11) - priority (11)
- priority= (11)
- raise (11)
-
report
_ on _ exception (16) -
report
_ on _ exception= (16) - run (11)
-
safe
_ level (7) -
set
_ trace _ func (11) - start (11)
- status (11)
- stop (11)
- stop? (11)
- terminate (11)
-
thread
_ variable _ get (11) -
thread
_ variable _ set (11) -
to
_ s (7) - value (11)
- wakeup (11)
検索結果
先頭5件
-
Thread
. new(*arg) {|*arg| . . . } -> Thread (18116.0) -
スレッドを生成して、ブロックの評価を開始します。 生成したスレッドを返します。
...始と同時にその
スレッド固有のローカル変数に値を渡すために使用します。
@raise ThreadError 現在のスレッドが属する ThreadGroup が freeze されている場合に発生します。またブロックを与えられずに呼ばれた場合にも発......のコードは間違いです。スレッドの実行が開始される前に
変数 i が書き変わる可能性があるからです。
for i in 1..5
Thread.new { p i }
end
上の例は以下のように書き直すべきです。
for i in 1..5
Thread.new(i) {|t| p t }
end... -
Thread
# report _ on _ exception=(newstate) (115.0) -
真の場合、そのスレッドが例外によって終了した時に、その内容を $stderr に報告します。
...ルトはスレッド作成時の Thread.report_on_exception です。
@param newstate スレッド実行中に例外発生した場合、その内容を報告するかどうかを true か false で指定します。
//emlist[例][ruby]{
a = Thread.new{ Thread.stop; raise }
a.report_on_exception......true
a.run
# => #<Thread:0x00007fc3f48c7908@(irb):1 run> terminated with exception (report_on_exception is true):
# Traceback (most recent call last):
# (irb):1:in `block in irb_binding': unhandled exception
# #<Thread:0x00007fc3f48c7908@(irb):1 dead>
b = Thread.new{ Thread.stop; raise }
b.......report_on_exception = false
b.run # => #<Thread:0x00007fc3f48aefc0@(irb):4 dead>
//}
@see Thread.report_on_exception... -
Thread
# abort _ on _ exception=(newstate) (109.0) -
真の場合、そのスレッドが例外によって終了した時に、インタプリタ 全体を中断させます。false の場合、あるスレッドで起こった例 外は、Thread#join などで検出されない限りそのスレッ ドだけをなにも警告を出さずに終了させます。
...スレッドで起こった例
外は、Thread#join などで検出されない限りそのスレッ
ドだけをなにも警告を出さずに終了させます。
デフォルトは偽です。c:Thread#exceptionを参照してください。
@param newstate 自身を実行中に例外発生し......た場合、インタプリタ全体を終了させるかどうかを true か false で指定します。
//emlist[例][ruby]{
thread = Thread.new { sleep 1 }
thread.abort_on_exception # => false
thread.abort_on_exception = true
thread.abort_on_exception # => true
//}... -
Thread
. report _ on _ exception=(newstate) (109.0) -
真の時は、いずれかのスレッドが例外によって終了した時に、その内容を $stderr に報告します。
...了した時に、その内容を $stderr に報告します。
デフォルトは false です。
Thread.new { 1.times { raise } }
は $stderr に以下のように出力します:
#<Thread:...> terminated with exception (report_on_exception is true):
Traceback (most recent call last):......スレッドが終了しないようにするのがより良い方法です。
* Thread#join や Thread#value でそのスレッドの終了を待つことが保証できるなら、
スレッド開始時に Thread.current.report_on_exception = false でレポートを無効化しても
安......を待つことができなかったりするかもしれません。
スレッドごとに設定する方法は Thread#report_on_exception= を参照してください。
@param newstate スレッド実行中に例外発生した場合、その内容を報告するかどうかを true か false......終了した時に、その内容を $stderr に報告します。
デフォルトは true です。
Thread.new { 1.times { raise } }
は $stderr に以下のように出力します:
#<Thread:...> terminated with exception (report_on_exception is true):
Traceback (most recent call last):... -
Thread
. abort _ on _ exception=(newstate) (103.0) -
真の時は、いずれかのスレッドが例外によって終了した時に、インタプリタ 全体を中断させます。false の場合、あるスレッドで起こった例外は、Thread#join などで検出されない限りそのスレッドだけをなにも警告を出さずに終了させます。
...スレッドで起こった例外は、Thread#join
などで検出されない限りそのスレッドだけをなにも警告を出さずに終了させます。
デフォルトは false です。
c:Thread#exceptionを参照してください。
@param newstate スレッド実行中に例外発......生した場合、インタプリタ全体を終了させるかどうかを true か false で指定します。
//emlist[例][ruby]{
Thread.abort_on_exception # => false
Thread.abort_on_exception = true
Thread.abort_on_exception # => true
//}... -
Thread
# [](name) -> object | nil (27.0) -
name に対応したスレッドに固有のデータを取り出します。 name に対応するスレッド固有データがなければ nil を返し ます。
...emlist[例][ruby]{
[
Thread.new { Thread.current["name"] = "A" },
Thread.new { Thread.current[:name] = "B" },
Thread.new { Thread.current["name"] = "C" }
].each do |th|
th.join
puts "#{th.inspect}: #{th[:name]}"
end
# => #<Thread:0x00000002a54220 dead>: A
# => #<Thread:0x00000002a541a8 d......30 dead>: C
//}
Thread#[] と Thread#[]= を用いたスレッド固有の変数は
Fiber を切り替えると異なる変数を返す事に注意してください。
//emlist[][ruby]{
def meth(newvalue)
begin
oldvalue = Thread.current[:name]
Thread.current[:name] = newvalue
yield......new {
meth(1) {
Fiber.yield
}
}
meth(2) {
f.resume
}
f.resume
p Thread.current[:name]
# => nil if fiber-local
# => 2 if thread-local (The value 2 is leaked to outside of meth method.)
//}
Fiber を切り替えても同じ変数を返したい場合は
Thread#thread_variable_get と Thread... -
Thread
# status -> String | false | nil (27.0) -
生きているスレッドの状態を文字列 "run"、"sleep", "aborting" のいず れかで返します。正常終了したスレッドに対して false、例外によ り終了したスレッドに対して nil を返します。
...て nil を返します。
Thread#alive? が真を返すなら、このメソッドも真です。
例:
a = Thread.new { raise("die now") }
b = Thread.new { Thread.stop }
c = Thread.new { Thread.exit }
d = Thread.new { sleep }
d.kill #=> #<Thread:0x401b3678 aborting>
a.......status #=> nil
b.status #=> "sleep"
c.status #=> false
d.status #=> "aborting"
Thread.current.status #=> "run"
@see Thread#alive?, Thread#stop?... -
Thread
# join -> self (21.0) -
スレッド self の実行が終了するまで、カレントスレッドを停止し ます。self が例外により終了していれば、その例外がカレントス レッドに対して発生します。
...raise ThreadError join を実行することによってデッドロックが起きる場合に発生します。またカレントスレッドを join したときにも発生します。
以下は、生成したすべてのスレッドの終了を待つ例です。
threads = []
threads.pus......h(Thread.new { n = rand(5); sleep n; n })
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.each {|t| t.join}... -
Thread
# join(limit) -> self | nil (21.0) -
スレッド self の実行が終了するまで、カレントスレッドを停止し ます。self が例外により終了していれば、その例外がカレントス レッドに対して発生します。
...raise ThreadError join を実行することによってデッドロックが起きる場合に発生します。またカレントスレッドを join したときにも発生します。
以下は、生成したすべてのスレッドの終了を待つ例です。
threads = []
threads.pus......h(Thread.new { n = rand(5); sleep n; n })
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.each {|t| t.join}... -
Thread
# value -> object (21.0) -
スレッド self が終了するまで待ち(Thread#join と同じ)、 そのスレッドのブロックが返した値を返します。スレッド実行中に例外が 発生した場合には、その例外を再発生させます。
...ッド self が終了するまで待ち(Thread#join と同じ)、
そのスレッドのブロックが返した値を返します。スレッド実行中に例外が
発生した場合には、その例外を再発生させます。
スレッドが Thread#kill によって終了した場合は、......の終了を待ち結果を出力する例です。
threads = []
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.each {|t| p t.value}
最後の行で、待ち合......わせを行っていることがわかりにくいと思うなら以下
のように書くこともできます。
threads.each {|t| p t.join.value}...