.
-, :
Thread.new { Thread.current.abort_on_exception = true; raise 'Oh, no!' }
puts 'Ready or not, here I come'
.
-, , , , #join:
gollum = Thread.new { raise 'My precious!!!' }
begin
gollum.join
rescue => e
puts e.message
end
, . , - . , , , . threads.each(&:join) , - :
frodo = Thread.new { raise 'Oh, no, Frodo!' }
sam = Thread.new { raise 'Oh, no, Sam!' }
begin
[frodo, sam].each(&:join)
rescue => e
puts e.message
end
puts 'This is the end, my only friend, the end.'
, , !
, , .
:
frodo = Thread.new { Thread.current.abort_on_exception = true; raise 'Oh, no, Frodo!' }
sam = Thread.new { Thread.current.abort_on_exception = true; raise 'Oh, no, Sam!' }
begin
[frodo, sam].each(&:join)
rescue => e
puts e.message
end
puts 'This is the end, my only friend, the end.'
. , ( ), , , , . (, , ) , .
:
ex.rb: 1: in `block in ': , , ! (RuntimeError)
, .
, , !
, , .
, , . , , . , .
, , ! ex.rb: 2: : , , ! (RuntimeError)
. , . , .
, , !
, , end.ex.rb: 1: in `block in ': , , ! (RuntimeError)
( ) . , . . , , , .
, , , . , , , :
def execute_safely_concurrently(number_of_threads, &work)
return if number_of_threads.zero?
begin
Thread.new(&work).join
rescue => e
puts e
end
execute_safely_concurrently(number_of_threads.pred, &work)
end
execute_safely_concurrently(2) do
Thread.current.abort_on_exception = true
raise 'Handle me, bitte!'
end