Skip to content

Instantly share code, notes, and snippets.

@kazjote
Created May 13, 2013 11:35
Show Gist options
  • Save kazjote/5567712 to your computer and use it in GitHub Desktop.
Save kazjote/5567712 to your computer and use it in GitHub Desktop.
Ruby threads Threads are executed in parallel while waiting for external command to finish.
Using /home/kazjote/.rvm/gems/ruby-1.9.3-p327
➜ threads time ruby threads.rb 1
ruby threads.rb 1 0.02s user 0.01s system 0% cpu 10.054 total
➜ threads time ruby threads.rb 2
ruby threads.rb 2 0.02s user 0.02s system 0% cpu 6.049 total
➜ threads time ruby threads.rb 4
ruby threads.rb 4 0.01s user 0.02s system 0% cpu 4.040 total
➜ threads time ruby threads.rb 8
ruby threads.rb 8 0.02s user 0.02s system 1% cpu 3.037 total
➜ threads time ruby threads.rb 16
ruby threads.rb 16 0.02s user 0.05s system 3% cpu 2.065 total
➜ threads time ruby threads.rb 32
ruby threads.rb 32 0.05s user 0.05s system 4% cpu 2.073 total
tasks = 10
threads = Array.new(ARGV.first.to_i) do
Thread.new do
while tasks > 0
`sleep 1`
tasks -= 1
end
end
end
threads.each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment