Created
May 2, 2011 21:19
-
-
Save felipeelias/952389 to your computer and use it in GitHub Desktop.
threads
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
require 'open-uri' | |
def link_valid?(url) | |
open(url) | |
true | |
rescue | |
false | |
end | |
def validate_urls(urls) | |
urls = urls.dup | |
threads = 3.times.map do |url_subset| | |
Thread.new do | |
while !urls.empty? | |
url = urls.pop | |
yield url, link_valid?(url) | |
end | |
end | |
end | |
p threads.map(&:join) | |
end | |
urls = %w( | |
http://rubysource.com | |
http://xaviershay.com | |
http://twitter.com | |
not_a_link | |
http://bogus.bogus | |
) | |
validate_urls(urls) do |url, valid| | |
puts "#{url} was #{valid ? "valid" : "invalid"}" | |
end |
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
require 'thread' | |
queue = Queue.new | |
# with an array, this will give some problems | |
# queue = [] | |
producer = Thread.new do | |
5.times do |i| | |
sleep rand(i) # simulate expense | |
queue << i | |
puts "#{i} produced" | |
end | |
end | |
consumer = Thread.new do | |
5.times do |i| | |
value = queue.pop | |
sleep rand(i/2) # simulate expense | |
puts "consumed #{value}" | |
end | |
end | |
consumer.join |
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
require 'open-uri' | |
def link_valid?(url) | |
open(url) | |
true | |
rescue | |
false | |
end | |
def validate_urls(urls) | |
number_of_threads = (urls.length / 3.0).round # 3 threads | |
puts "Creating #{number_of_threads} threads" | |
threads = urls.each_slice(number_of_threads).map do |url_subset| | |
Thread.new do | |
url_subset.each do |url| | |
yield url, link_valid?(url) | |
end | |
end | |
end | |
p threads.map(&:join) | |
end | |
urls = %w( | |
http://rubysource.com | |
http://xaviershay.com | |
http://twitter.com | |
not_a_link | |
http://bogus.bogus | |
) | |
validate_urls(urls) do |url, valid| | |
puts "#{url} was #{valid ? "valid" : "invalid"}" | |
end |
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
require 'open-uri' | |
require 'thread' | |
def link_valid?(url) | |
open(url) | |
true | |
rescue | |
false | |
end | |
def validate_urls(urls) | |
queue = Queue.new | |
urls.each { |u| queue << u } | |
threads = 5.times.map do | |
Thread.new do | |
while !queue.empty? | |
url = queue.pop | |
yield url, link_valid?(url) | |
end | |
end | |
end | |
p threads.map(&:join) | |
end | |
urls = %w( | |
http://rubysource.com | |
http://xaviershay.com | |
http://twitter.com | |
not_a_link | |
http://bogus.bogus | |
) | |
validate_urls(urls) do |url, valid| | |
puts "#{url} was #{valid ? "valid" : "invalid"}" | |
end |
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
require 'open-uri' | |
require 'thread' | |
def link_valid?(url) | |
open(url) | |
true | |
rescue | |
false | |
end | |
def validate_urls(urls) | |
queue = Queue.new | |
semaphore = Mutex.new | |
urls.each { |u| queue << u } | |
threads = 5.times.map do | |
Thread.new do | |
while !queue.empty? | |
url = queue.pop | |
valid = link_valid?(url) | |
semaphore.synchronize { | |
yield url, valid | |
} | |
end | |
end | |
end | |
p threads.map(&:join) | |
end | |
urls = %w( | |
http://rubysource.com | |
http://xaviershay.com | |
http://twitter.com | |
not_a_link | |
http://bogus.bogus | |
) | |
validate_urls(urls) do |url, valid| | |
puts "#{url} was #{valid ? "valid" : "invalid"}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment