るりまサーチ

最速Rubyリファレンスマニュアル検索!
92件ヒット [1-92件を表示] (0.013秒)
トップページ > クエリ:j[x] > クラス:Thread[x]

別のキーワード

  1. encoding windows_31j
  2. _builtin windows_31j
  3. _builtin cswindows31j
  4. encoding cswindows31j
  5. json j

ライブラリ

キーワード

検索結果

Thread#join -> self (6101.0)

スレッド self の実行が終了するまで、カレントスレッドを停止し ます。self が例外により終了していれば、その例外がカレントス レッドに対して発生します。

...raise ThreadError join を実行することによってデッドロックが起きる場合に発生します。またカレントスレッドを join したときにも発生します。

以下は、生成したすべてのスレッドの終了を待つ例です。

thread
s = []
thread
s.pus...
...h(Thread.new { n = rand(5); sleep n; n })
thread
s.push(Thread.new { n = rand(5); sleep n; n })
thread
s.push(Thread.new { n = rand(5); sleep n; n })

thread
s.each {|t| t.join}...

Thread#join(limit) -> self | nil (6101.0)

スレッド self の実行が終了するまで、カレントスレッドを停止し ます。self が例外により終了していれば、その例外がカレントス レッドに対して発生します。

...raise ThreadError join を実行することによってデッドロックが起きる場合に発生します。またカレントスレッドを join したときにも発生します。

以下は、生成したすべてのスレッドの終了を待つ例です。

thread
s = []
thread
s.pus...
...h(Thread.new { n = rand(5); sleep n; n })
thread
s.push(Thread.new { n = rand(5); sleep n; n })
thread
s.push(Thread.new { n = rand(5); sleep n; n })

thread
s.each {|t| t.join}...

Thread#[](name) -> object | nil (101.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...
...0000002a54130 dead>: C
//}

Thread
#[] と Thread#[]= を用いたスレッド固有の変数は
Fiber を切り替えると異なる変数を返す事に注意してください。

//emlist[][ruby]{
def meth(newvalue)
begin
oldvalue = Thread.current[:name]
Thread
.current[:name] = newvalu...
....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_variable_set
を使用してください。

Thread
#[]=...
...ume
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_variable_set
を使用してください。

@see Thread#fet...

Thread#fetch(name, default = nil) {|name| ... } -> object (101.0)

name に関連づけられたスレッドに固有のデータを返します。 name に対応するスレッド固有データがない時には、引数 default が 与えられていればその値を、ブロックが与えられていれば そのブロックを評価した値を返します。

...と発生します。

//emlist[例][ruby]{
th = Thread.new { Thread.current[:name] = 'A' }
th.join
th.fetch(:name) # => "A"
th.fetch(:fetch, 'B') # => "B"
th.fetch('name') {|name| "Thread" + name} # => "A"
th.fetch('fetch') {|name| "Thread" + name} # => "Threadfetch"
//}

@see Thread#[]...

Thread#thread_variable_get(key) -> object | nil (101.0)

引数 key で指定した名前のスレッドローカル変数を返します。

...注意]: Thread#[] でセットしたローカル変数(Fiber ローカル変数)と
異なり、Fiber を切り替えても同じ変数を返す事に注意してください。

例:

Thread
.new {
Thread
.current.thread_variable_set("foo", "bar") # スレッドローカル
Thread
.current...
...
Thread
.current.thread_variable_get("foo"), # スレッドローカル
Thread
.current["foo"], # Fiber ローカル
]
}.resume
}.join.value # => ['bar', nil]

この例の "bar" は Thread#thread_variable_get により得られ
た値で、nil はThread...
...#[] により得られた値です。

@see Thread#thread_variable_set, Thread#[]

@see https://magazine.rubyist.net/articles/0041/0041-200Special-note.html...

絞り込み条件を変える

Thread#value -> object (101.0)

スレッド self が終了するまで待ち(Thread#join と同じ)、 そのスレッドのブロックが返した値を返します。スレッド実行中に例外が 発生した場合には、その例外を再発生させます。

...ッド self が終了するまで待ち(Thread#join と同じ)、
そのスレッドのブロックが返した値を返します。スレッド実行中に例外が
発生した場合には、その例外を再発生させます。

スレッドが Thread#kill によって終了した場合は、...
...の終了を待ち結果を出力する例です。

thread
s = []
thread
s.push(Thread.new { n = rand(5); sleep n; n })
thread
s.push(Thread.new { n = rand(5); sleep n; n })
thread
s.push(Thread.new { n = rand(5); sleep n; n })

thread
s.each {|t| p t.value}

最後の行で、待ち合...
...わせを行っていることがわかりにくいと思うなら以下
のように書くこともできます。

thread
s.each {|t| p t.join.value}...

Thread.exclusive { ... } -> object (101.0)

VM グローバルの Mutex をロックし、ブロックを実行します。

...ます。

このクラスメソッドの挙動は 1.8 以前とは違います。
Thread
.exclusive は VM グローバルの Thread::MUTEX_FOR_THREAD_EXCLUSIVE の
synchronize を呼び出しているだけで、Thread.exclusive していないスレッドは動きます。
Mutex や Monitor など...
...ッドの挙動は 1.8 以前とは違います。
Thread
.exclusive は VM グローバルの Thread::MUTEX_FOR_THREAD_EXCLUSIVE の
synchronize を呼び出しているだけで、Thread.exclusive していないスレッドは動きます。
Thread
::Mutex や Monitor などの他の排他制御の...
...クラスメソッドの挙動は 1.8 以前とは違います。
Thread
.exclusive は VM グローバルの Mutex の
synchronize を呼び出しているだけで、Thread.exclusive していないスレッドは動きます。
Thread
::Mutex や Monitor などの他の排他制御の方法を検...

Thread.handle_interrupt(hash) { ... } -> object (101.0)

スレッドの割り込みのタイミングを引数で指定した内容に変更してブロックを 実行します。

...ングを引数で指定した内容に変更してブロックを
実行します。

「割り込み」とは、非同期イベントや Thread#raise や
Thread
#kill、Signal.#trap(未サポート)、メインスレッドの終了
(メインスレッドが終了すると、他のスレッドも終...
...してください。

=== 使い方

例:Thread#raise 発生のタイミングを制御する例

th = Thread.new do
Thread
.handle_interrupt(RuntimeError => :never) {
begin
# 安全にリソースの割り当てが可能
Thread
.handle_interrupt(RuntimeError => :immediate...
...) {
# ...
}
ensure
# 安全にリソースの解放が可能
end
}
end
Thread
.pass
# ...
th.raise "stop"

RuntimeError を無視(延期)している間はリソースの割り当てや ensure
節でリソースの解放を安全に行う事ができ...