|
|
||
---------------------------------------------------------- Class: Thread Thread encapsulates the behavior of a thread of execution, including the main thread of the Ruby script. In the descriptions of the methods in this class, the parameter _sym_ refers to a symbol, which is either a quoted string or a Symbol (such as :name ). ------------------------------------------------------------------------ Class methods: -------------- abort_on_exception, abort_on_exception=, critical, critical=, current, exclusive, exit, fork, kill, list, main, new, new, pass, start, stop Instance methods: ----------------- , =, abort_on_exception, abort_on_exception=, alive?, exit, group, inspect, join, key?, keys, kill, priority, priority=, raise, run, safe_level, status, stop?, terminate, value, wakeup
==== Thread ==== Ruby のスレッドを表現するクラスです。 Thread を使うことで並行プログラミングが可能になります。スレッド はメモリ空間を共有して同時に実行される制御の流れです。ただし、現在の実 装では Ruby インタプリタは時分割でスレッドを実行しますので、スレッドを 使うことで実行速度が速くなることはありません。 プログラムの開始と同時に生成されるスレッドを*1 と呼 びます。なんらかの理由でメインスレッドが終了する時には、他の全てのスレッ ドもプログラム全体も終了します。ユーザからの割込みによって発生した例外 はメインスレッドに送られます。 スレッドの起動時に指定したブロックの実行が終了するとスレッドの実行も終 了します。ブロックの終了は正常な終了も例外などによる異常終了も含みます。 Ruby のスレッドスケジューリングは優先順位付のラウンドロビンです。一定 時間毎、あるいは実行中のスレッドが権利を放棄したタイミングでスケジュー リングが行われ、その時点で実行可能なスレッドのうち最も優先順位が高いも のにコンテキストが移ります。 : スレッドと例外 あるスレッドで例外が発生し、そのスレッド内で rescue で捕捉されなかっ た場合、通常はそのスレッドだけがなにも警告なしに終了されます。ただ しその例外で終了するスレッドを Thread#join で待っている他の スレッドがある場合、その待っているスレッドに対して、同じ例外が再度 発生します。 begin t = Thread.new do Thread.pass # メインスレッドが確実にjoinするように raise "unhandled exception" end t.join rescue p $! # => "unhandled exception" end また、以下の 3 つの方法により、いずれかのスレッドが例外によって終 了した時に、インタプリタ全体を中断させるように指定することができま す。 * 組み込み変数 $DEBUG を真に設定する(デバッグモード) ruby インタプリタを -d 付きで起動した場合も同様。 * Thread.abort_on_exception でフラグを設定する。 * Thread#abort_on_exception [Thread/abort_on_exception] で指定 したスレッドのフラグを設定する。 上記3つのいずれかが設定されていた場合、インタプリタ全体が中断されます。 : スレッドの状態 個々のスレッドは、以下の実行状態を持ちます。これらの状態は Object#inspect [Object/inspect] や Thread#status [Thread/status] によって見ることができます。 p Thread.new {sleep 1} # => #<Thread:0xa039de0 sleep> : run (実行or実行可能状態) 生成されたばかりのスレッドや run や wakeup で起こされたスレッドはこの状態です。 join でスレッドの終了を待っているスレッドもスレッドの 終了によりこの状態になります。 この状態のスレッドは「生きて」います。 : sleep (停止状態) Thread.stop や join により停止されたスレッ ドはこの状態になります。 この状態のスレッドは「生きて」います。 : aborting (終了処理中) kill 等で終了されるスレッドは一時的にこの状態になりま す。この状態から停止状態(stop)になることもあります。 この状態のスレッドはまだ「生きて」います。 : dead (終了状態) kill 等で終了したスレッドはこの状態になります。この状 態のスレッドはどこからも参照されていなければ GC によりメモリ上から なくなります。 この状態のスレッドは「死んで」います。 ---- Singleton methods ---- abort_on_exception abort_on_exception= critical critical= current exit fork kill list main new pass start stop ---- Instance methods ---- [] abort_on_exception abort_on_exception= alive? exit group join key? keys kill priority priority= raise run safe_level status stop? terminate value wakeup ---- Singleton methods (inherited) ---- ---- Instance methods (inherited) ---- == === =~ __id__ __send__ _dump _load class clone display dup eql? equal? extend freeze frozen? hash id initialize initialize_copy inspect instance_eval instance_of? instance_variable_get instance_variable_set instance_variables is_a? kind_of? marshal_dump marshal_load method method_missing methods nil? object_id pretty_print pretty_print_cycle pretty_print_instance_variables private_methods protected_methods public_methods remove_instance_variable respond_to? send singleton_method_added singleton_method_removed singleton_method_undefined singleton_methods taint tainted? to_a to_ary to_hash to_int to_s to_str type untaint