Created
February 21, 2012 06:49
-
-
Save vessi/1874499 to your computer and use it in GitHub Desktop.
Threads creation with outer join (working)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# THIS IS WRONG! | |
start_time = Time.new | |
10.times do | |
t = Thread.new do |th| | |
sec = rand(1..5) | |
puts "Run for #{sec} s" | |
sleep(sec) | |
#th.join #first case | |
puts "slept" | |
end | |
t.join #second case | |
end | |
puts "Execution time #{Time.new - start_time}" | |
#Execution in first case: | |
#ruby threads_inner_join.rb | |
#Run for 5 s | |
#Run for 5 s | |
#Run for 5 s | |
#Run for 3 s | |
#Run for 4 s | |
#Run for 3 s | |
#Run for 3 s | |
#Run for 4 s | |
#Run for 5 s | |
#Run for 3 s | |
#Execution time 40.026031. Это же очевидно, почему так - треды выполняются по очереди | |
#Execution in commented second case and uncommented first | |
#No output when ruby except Execution time 0.002 - not working | |
#In irb: | |
#threads_inner_join.rb(main):013:0* Run for 3 s | |
#Run for 1 s | |
#Run for 5 s | |
#Run for 3 s | |
#Run for 4 s | |
#Run for 1 s | |
#Run for 3 s | |
#Run for 3 s | |
#Run for 1 s | |
#Run for 5 s | |
#puts "Execution time #{Time.new - start_time}" | |
#Execution time 0.033001 - threads not working |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
threads = [] | |
start_time = Time.new | |
10.times do | |
threads << Thread.new do | |
st = rand(1..5) | |
puts "Sleep for #{st}" | |
sleep(st) | |
end | |
end | |
threads.each { |t| t.join } | |
exec_time = Time.new - start_time | |
puts "Execution time #{exec_time}" | |
#Execution:ruby threads_outer_join.rb | |
#Sleep for 1 | |
#Sleep for 5 | |
#Sleep for 1 | |
#Sleep for 3 | |
#Sleep for 3 | |
#Sleep for 2 | |
#Sleep for 3 | |
#Sleep for 1 | |
#Sleep for 2 | |
#Sleep for 5 | |
#Execution time 5.016278 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment