-
-
Save manveru/5717144 to your computer and use it in GitHub Desktop.
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
#!/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