るりまサーチ (Ruby 2.7.0)

最速Rubyリファレンスマニュアル検索!
38件ヒット [1-38件を表示] (0.112秒)

検索結果

Thread#[](name) -> object | nil (7.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...
...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#[]=(name,val) (7.0)

val を name に対応するスレッド固有のデータとして格納します。

...ーを文字列か Symbol で指定します。文字列を指定した場合は String#to_sym によりシンボルに変換されます。

@param val スレッド固有データを指定します。nil を指定するとそのスレッド固有データは削除されます。


@see Thread#[]...

Thread#abort_on_exception -> bool (7.0)

真の場合、そのスレッドが例外によって終了した時に、インタプリタ 全体を中断させます。false の場合、あるスレッドで起こった例 外は、Thread#join などで検出されない限りそのスレッ ドだけをなにも警告を出さずに終了させます。

...。false の場合、あるスレッドで起こった例
外は、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#abort_on_exception=(newstate) (7.0)

真の場合、そのスレッドが例外によって終了した時に、インタプリタ 全体を中断させます。false の場合、あるスレッドで起こった例 外は、Thread#join などで検出されない限りそのスレッ ドだけをなにも警告を出さずに終了させます。

...。false の場合、あるスレッドで起こった例
外は、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#add_trace_func(pr) -> Proc (7.0)

スレッドにトレース用ハンドラを追加します。

...加します。

追加したハンドラを返します。

@param pr トレースハンドラ(Proc オブジェクト)

//emlist[例][ruby]{
th = Thread.new do
class Trace
end
43.to_s
end
th.add_trace_func lambda {|*arg| p arg }
th.join

# => ["line", "example.rb", 4, nil, #<Binding:0x00007f...
...e0>, nil]
# => ["line", "example.rb", 6, nil, #<Binding:0x00007f98e108d4b0>, nil]
# => ["c-call", "example.rb", 6, :to_s, #<Binding:0x00007f98e1097aa0>, Integer]
# => ["c-return", "example.rb", 6, :to_s, #<Binding:0x00007f98e1095cc8>, Integer]
//}

@see Thread#set_trace_func Kernel.#set_trace_func...

絞り込み条件を変える

Thread#alive? -> bool (7.0)

スレッドが「生きている」時、true を返します。

...」時、true を返します。

例:
thr = Thread.new { }
thr.join # => #<Thread:0x401b3fb0 dead>
Thread
.current.alive? # => true
thr.alive? # => false

Thread
#status が真を返すなら、このメソッドも真です。

@see Thread#status, Thread#stop?...

Thread#backtrace -> [String] | nil (7.0)

スレッドの現在のバックトレースを返します。

...している場合は nil を返します。

//emlist[例][ruby]{
class C1
def m1
sleep 5
end
def m2
m1
end
end

th = Thread.new {C1.new.m2; Thread.stop}
th.backtrace
# => [
# [0] "(irb):3:in `sleep'",
# [1] "(irb):3:in `m1'",
# [2] "(irb):6:in `m2'",
# [3] "(irb...

Thread#backtrace_locations(range) -> [Thread::Backtrace::Location] | nil (7.0)

スレッドの現在のバックトレースを Thread::Backtrace::Location の配 列で返します。

...スレッドの現在のバックトレースを Thread::Backtrace::Location の配
列で返します。

引数で指定した値が範囲外の場合、スレッドがすでに終了している場合は nil
を返します。

@param start 開始フレームの位置を数値で指定します...
...似ていますが、本メソッドは self に限定
した情報を返します。

//emlist[例][ruby]{
thread
= Thread.new { sleep 1 }
thread
.run
thread
.backtrace_locations # => ["/path/to/test.rb:1:in `sleep'", "/path/to/test.rb:1:in `block in <main>'"]
//}

@see Thread::Backtrace::Location...

Thread#backtrace_locations(start = 0, length = nil) -> [Thread::Backtrace::Location] | nil (7.0)

スレッドの現在のバックトレースを Thread::Backtrace::Location の配 列で返します。

...スレッドの現在のバックトレースを Thread::Backtrace::Location の配
列で返します。

引数で指定した値が範囲外の場合、スレッドがすでに終了している場合は nil
を返します。

@param start 開始フレームの位置を数値で指定します...
...似ていますが、本メソッドは self に限定
した情報を返します。

//emlist[例][ruby]{
thread
= Thread.new { sleep 1 }
thread
.run
thread
.backtrace_locations # => ["/path/to/test.rb:1:in `sleep'", "/path/to/test.rb:1:in `block in <main>'"]
//}

@see Thread::Backtrace::Location...

Thread#exit -> self (7.0)

スレッドの実行を終了させます。終了時に ensure 節が実行されます。

...レッドの Thread#value の返り値は不定です。
自身がメインスレッドであるか最後のスレッドである場合は、プロセスを Kernel.#exit(0)
により終了します。

Kernel.#exit と違い例外 SystemExit を発生しません。

th1 = Thread.new do
begi...

絞り込み条件を変える

Thread#fetch(name, default = nil) {|name| ... } -> object (7.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#group -> ThreadGroup (7.0)

スレッドが属している ThreadGroup オブジェクトを返します。

...スレッドが属している ThreadGroup オブジェクトを返します。

p Thread.current.group == ThreadGroup::Default
# => true...

Thread#inspect -> String (7.0)

自身を人間が読める形式に変換した文字列を返します。

...自身を人間が読める形式に変換した文字列を返します。

//emlist[例][ruby]{
a = Thread.current
a.inspect # => "#<Thread:0x00007fdbaf07ddb0 run>"
b = Thread.new{}
b.inspect # => "#<Thread:0x00007fdbaf8f7d10@(irb):3 dead>"
//}...

Thread#join -> self (7.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 (7.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#key?(name) -> bool (7.0)

name に対応したスレッドに固有のデータが定義されていれば true を返します。

...name に対応したスレッドに固有のデータが定義されていれば
true を返します。

@param name 文字列か Symbol で指定します。

//emlist[例][ruby]{
me = Thread.current
me[:oliver] = "a"
me.key?(:oliver) # => true
me.key?(:stanley) # => false
//}...

Thread#keys -> [Symbol] (7.0)

スレッド固有データに関連づけられたキーの配列を返します。キーは Symbol で返されます。

...スレッド固有データに関連づけられたキーの配列を返します。キーは
Symbol で返されます。

th = Thread.current
th[:foo] = 'FOO'
th['bar'] = 'BAR'
p th.keys

#=> [:bar, :foo]...

Thread#kill -> self (7.0)

スレッドの実行を終了させます。終了時に ensure 節が実行されます。

...レッドの Thread#value の返り値は不定です。
自身がメインスレッドであるか最後のスレッドである場合は、プロセスを Kernel.#exit(0)
により終了します。

Kernel.#exit と違い例外 SystemExit を発生しません。

th1 = Thread.new do
begi...

Thread#name -> String (7.0)

self の名前を返します。

...self の名前を返します。


@see Thread#name=...

Thread#name=(name) -> String (7.0)

self の名前を name に設定します。

...ームによっては pthread やカーネルにも設定を行う場合があります。

@raise ArgumentError 引数に ASCII 互換ではないエンコーディングのものを
指定した場合に発生します。

//emlist[例][ruby]{
a = Thread.new{}
a.name = 'named'...
...a.name # => "named"
a.inspect # => "#<Thread:0x00007f85ac8721f0@named@(irb):1 dead>"
//}

@see Thread#name...

絞り込み条件を変える

Thread#pending_interrupt?(error = nil) -> bool (7.0)

self の非同期例外のキューが空かどうかを返します。

...self の非同期例外のキューが空かどうかを返します。

@param error 対象の例外クラスを指定します。


@see Thread.pending_interrupt?...

Thread#priority -> Integer (7.0)

スレッドの優先度を返します。この値が大きいほど優先度が高くなります。 メインスレッドのデフォルト値は 0 です。新しく生成されたスレッドは親スレッドの priority を引き継ぎます。

...定します。プラットフォームに依存します。

//emlist[例][ruby]{
Thread
.current.priority # => 0

count1 = count2 = 0
a = Thread.new do
loop { count1 += 1 }
end
a.priority = -1

b = Thread.new do
loop { count2 += 1 }
end
b.priority = -2
count1 = count2 = 0 # re...

Thread#priority=(val) (7.0)

スレッドの優先度を返します。この値が大きいほど優先度が高くなります。 メインスレッドのデフォルト値は 0 です。新しく生成されたスレッドは親スレッドの priority を引き継ぎます。

...定します。プラットフォームに依存します。

//emlist[例][ruby]{
Thread
.current.priority # => 0

count1 = count2 = 0
a = Thread.new do
loop { count1 += 1 }
end
a.priority = -1

b = Thread.new do
loop { count2 += 1 }
end
b.priority = -2
count1 = count2 = 0 # re...

Thread#raise(error_type, message, traceback) -> () (7.0)

自身が表すスレッドで強制的に例外を発生させます。

...してください。

@param message Kernel.#raise を参照してください。

@param traceback Kernel.#raise を参照してください。

Thread
.new {
sleep 1
Thread
.main.raise "foobar"
}

begin
sleep
rescue
p $!, $@
end

=> #<RuntimeError: foobar>
["-:3"]...

Thread#report_on_exception -> bool (7.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#report_on_exception=(newstate) (7.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#run -> self (7.0)

停止状態(stop)のスレッドを再開させます。 Thread#wakeup と異なりすぐにスレッドの切り替え を行います。

...レッドを再開させます。
Thread
#wakeup と異なりすぐにスレッドの切り替え
を行います。

@raise ThreadError 死んでいるスレッドに対して実行すると発生します。

//emlist[例][ruby]{
a = Thread.new { puts "a"; Thread.stop; puts "c" }
sleep 0.1 while a....
...status!='sleep'
puts "Got here"
a.run
a.join
# => a
# => Got here
# => c
//}

@see Thread#wakeup, Thread.stop...

Thread#safe_level -> Integer (7.0)

セーフレベルを返します。

...obsolete になりました。
単純に $SAFE をチェックしてください。

セーフレベルについてはspec/safelevelを参照してください。

//emlist[例][ruby]{
thr = Thread.new { $SAFE = 1; sleep }
Thread
.current.safe_level # => 0
thr.safe_level # => 1
//}...

Thread#set_trace_func(pr) -> Proc | nil (7.0)

スレッドにトレース用ハンドラを設定します。

...nil を渡すとトレースを解除します。

設定したハンドラを返します。

//emlist[例][ruby]{
th = Thread.new do
class Trace
end
2.to_s
Thread
.current.set_trace_func nil
3.to_s
end
th.set_trace_func lambda {|*arg| p arg }
th.join

# => ["line", "example.rb", 2, nil,...
...fc8de967798>, Thread]
# => ["c-return", "example.rb", 5, :current, #<Binding:0x00007fc8de9673b0>, Thread]
# => ["c-call", "example.rb", 5, :set_trace_func, #<Binding:0x00007fc8de966fc8>, Thread]
//}

@param pr トレースハンドラ(Proc オブジェクト) もしくは nil
@see Thread#add_trace_f...

Thread#status -> String | false | nil (7.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#stop? -> bool (7.0)

スレッドが終了(dead)あるいは停止(stop)している時、true を返します。

...スレッドが終了(dead)あるいは停止(stop)している時、true を返します。

//emlist[例][ruby]{
a = Thread.new { Thread.stop }
b = Thread.current
a.stop? # => true
b.stop? # => false
//}

@see Thread#alive?, Thread#status...

Thread#terminate -> self (7.0)

スレッドの実行を終了させます。終了時に ensure 節が実行されます。

...レッドの Thread#value の返り値は不定です。
自身がメインスレッドであるか最後のスレッドである場合は、プロセスを Kernel.#exit(0)
により終了します。

Kernel.#exit と違い例外 SystemExit を発生しません。

th1 = Thread.new do
begi...

Thread#thread_variable?(key) -> bool (7.0)

引数 key で指定した名前のスレッドローカル変数が存在する場合に true、そ うでない場合に false を返します。

...= Thread.current
me.thread_variable_set(:oliver, "a")
me.thread_variable?(:oliver) # => true
me.thread_variable?(:stanley) # => false

[注意]: Thread#[] でセットしたローカル変数(Fiber ローカル変数)が
対象ではない事に注意してください。

@see Thread...
...#thread_variable_get, Thread#[]...

Thread#thread_variable_get(key) -> object | nil (7.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#thread_variable_set(key, value) (7.0)

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

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

//emlist[例][ruby]{
thr = Thread.new do
Thread
.current.thread_variable_set(:cat, 'meow')
Thread
.c...
...urrent.thread_variable_set("dog", 'woof')
end
thr.join # => #<Thread:0x401b3f10 dead>
thr.thread_variables # => [:dog, :cat]
//}

@see Thread#thread_variable_get, Thread#[]...

絞り込み条件を変える

Thread#to_s -> String (7.0)

自身を人間が読める形式に変換した文字列を返します。

...自身を人間が読める形式に変換した文字列を返します。

//emlist[例][ruby]{
a = Thread.current
a.inspect # => "#<Thread:0x00007fdbaf07ddb0 run>"
b = Thread.new{}
b.inspect # => "#<Thread:0x00007fdbaf8f7d10@(irb):3 dead>"
//}...

Thread#value -> object (7.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#wakeup -> self (7.0)

停止状態(stop)のスレッドを実行可能状態(run)にします。

...を実行可能状態(run)にします。

@raise ThreadError 死んでいるスレッドに対して実行すると発生します。

//emlist[例][ruby]{
c = Thread.new { Thread.stop; puts "hey!" }
sleep 0.1 while c.status!='sleep'
c.wakeup
c.join
# => "hey!"
//}

@see Thread#run, Thread.stop...