るりまサーチ

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

別のキーワード

  1. openssl new
  2. _builtin new
  3. rexml/document new
  4. resolv new
  5. socket new

ライブラリ

キーワード

検索結果

<< 1 2 3 ... > >>

Thread.new(*arg) {|*arg| ... } -> Thread (39227.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#thread_variable_get(key) -> object | nil (27198.0)

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

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

例:

Thread.new
{
Thread
.current.thread_variable_set("foo", "bar") # スレッドローカル
Thread
.current...
...

Fiber.new {
Fiber.yield [
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) (27174.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.kill(thread) -> Thread (21271.0)

指定したスレッド thread に対して Thread#exit を呼びます。終了したスレッドを返します。

...指定したスレッド thread に対して Thread#exit を呼びます。終了したスレッドを返します。

@param thread 終了したい Thread オブジェクトを指定します。

th = Thread.new do
end

p Thread.kill(th) #=> #<Thread:0x40221bc8 dead>...

Thread#report_on_exception=(newstate) (21167.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) (21159.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#[](name) -> object | nil (21157.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#backtrace_locations(range) -> [Thread::Backtrace::Location] | nil (21155.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 (21155.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#[](name) -> object | nil (21151.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.report_on_exception=(newstate) (21143.0)

真の時は、いずれかのスレッドが例外によって終了した時に、その内容を $stderr に報告します。

...終了した時に、その内容を $stderr に報告します。

デフォルトは true です。

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...
<< 1 2 3 ... > >>