このマニュアルは既にメンテナンスが終了したバージョンの Ruby を対象としています。 最新版のマニュアルへ

Ruby 1.8.7 リファレンスマニュアル

instance method Thread#exit!

exit! -> self[permalink][rdoc]
kill! -> self
terminate! -> self

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

ただし、スレッドは終了処理中(aborting)にはなりますが、直ちに終了するとは限りません。すでに終了している場合は何もしません。このメソッドにより終了したスレッドの Thread#value の返り値は不定です。自身がメインスレッドであるか最後のスレッドである場合は、プロセスを Kernel.#exit(0) により終了します。

th1 = Thread.new do
  begin
    sleep 10
  ensure
    p "th1: this will be displayed"
  end
end
th2 = Thread.new do
  begin
    sleep 10
  ensure
    p "th2: this will NOT be displayed"
  end
end

th1.kill
th2.kill!

#=> "th1: this will be displayed"

[SEE_ALSO] Thread#exit, Kernel.#exit, Kernel.#exit!