4件ヒット
[1-4件を表示]
(0.120秒)
検索結果
先頭4件
-
Thread
# value -> object (24049.0) -
スレッド self が終了するまで待ち([m:Thread#join] と同じ)、そのスレッドのブロックが返した値を返します。スレッド実行中に例外が発生した場合には、その例外を再発生させます。
...ド self が終了するまで待ち([m:Thread#join] と同じ)、そのスレッドのブロックが返した値を返します。スレッド実行中に例外が発生した場合には、その例外を再発生させます。
スレッドが [m:Thread#kill] によって終了した場合は......の終了を待ち結果を出力する例です。
```ruby
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}
```
最後の行で、待ち合わ......せを行っていることがわかりにくいと思うなら以下のように書くこともできます。
```ruby
threads.each {|t| p t.join.value}
```... -
Thread
# [](name) -> object | nil (24031.0) -
name に対応したスレッドに固有のデータを取り出します。 name に対応するスレッド固有データがなければ nil を返します。
...``ruby title="例"
[
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......0002a54130 dead>: C
```
[m:Thread#\[\]] と [m:Thread#\[\]=] を用いたスレッド固有の変数は
Fiber を切り替えると異なる変数を返す事に注意してください。
```ruby
def meth(newvalue)
begin
oldvalue = Thread.current[:name]
Thread.current[:name] = newvalue......h(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 を切り替えても同じ変数を返したい場合は
[m:Thread#thread_variable_get] と [m:Thread#thread_variable_set]
を使用し... -
Thread
# join -> self (24031.0) -
スレッド self の実行が終了するまで、カレントスレッドを停止します。self が例外により終了していれば、その例外がカレントスレッドに対して発生します。
...* `ThreadError` -- join を実行することによってデッドロックが起きる場合に発生します。またカレントスレッドを join したときにも発生します。
以下は、生成したすべてのスレッドの終了を待つ例です。
```ruby
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| t.join}
```... -
Thread
# join(limit) -> self | nil (24031.0) -
スレッド self の実行が終了するまで、カレントスレッドを停止します。self が例外により終了していれば、その例外がカレントスレッドに対して発生します。
...* `ThreadError` -- join を実行することによってデッドロックが起きる場合に発生します。またカレントスレッドを join したときにも発生します。
以下は、生成したすべてのスレッドの終了を待つ例です。
```ruby
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| t.join}
```...
