Skip to content

Instantly share code, notes, and snippets.

@jcoene
Created February 25, 2013 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jcoene/5034155 to your computer and use it in GitHub Desktop.
Save jcoene/5034155 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# This program adds up the numbers 1 through 10,000 using 100 threads.
# Challenge A: Does this solution have any problems? If so, explain.
# Challenge B: Propose an alternate implementation that does not use threads.
require "thread"
mt_result = 0
100.times.map do |n|
Thread.new do
base = 1 + (n * 100)
100.times do |i|
mt_result += base + i
end
end
end.map(&:join)
puts "The multi-threaded result is: #{mt_result}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment