Skip to content

Instantly share code, notes, and snippets.

@FooBarWidget
Created February 21, 2011 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FooBarWidget/836861 to your computer and use it in GitHub Desktop.
Save FooBarWidget/836861 to your computer and use it in GitHub Desktop.
require 'thread'
require 'open-uri'
LOCK = Mutex.new
$progress = 0
def thread_start
while true
open("http://www.microsoft.com/en/us/default.aspx") do |f|
f.read
end
inc_progress
end
end
def inc_progress
LOCK.synchronize do
1000.times do
File.open("/dev/null", "w") do |f|
f.write("hi")
end
end
end
LOCK.synchronize do
$progress += 1
puts "#{Thread.current.object_id.to_s(16)}: Progress = #{$progress}"
end
end
def start
threads = []
50.times do
thread = Thread.new do
thread_start
end
thread.abort_on_exception = true
threads << thread
end
threads.each do |thread|
thread.join
end
end
start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment