Created
July 15, 2013 15:42
-
-
Save joonty/6000969 to your computer and use it in GitHub Desktop.
Test for two threads accessing the same array, one thread iterating and the other appending.
This file contains 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
iterations = 0 | |
payloads = (1..1000).to_a | |
threads = [] | |
threads << Thread.new do | |
200.times do | |
add_num = rand(1000..2000) | |
puts "\nAdding #{add_num} to payload" | |
payloads << add_num | |
end | |
end | |
threads << Thread.new do | |
payloads.each do |p| | |
iterations += 1 | |
puts "Payload: #{p}" | |
end | |
end | |
threads.each(&:join) | |
puts "\n---" | |
puts "Iterations: #{iterations}" | |
puts "Final payload length: #{payloads.length}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment