Skip to content

Instantly share code, notes, and snippets.

@possatti
Created November 17, 2013 05:59
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 possatti/7509866 to your computer and use it in GitHub Desktop.
Save possatti/7509866 to your computer and use it in GitHub Desktop.
Exemplo do uso de threads em Ruby.
#!/usr/bin/ruby
# Exemplo tirado de http://www.tutorialspoint.com/ruby/ruby_multithreading.htm
def func1
i=0
while i<=2
sleep(2)
puts "func1 at: #{Time.now}"
i=i+1
end
end
def func2
j=0
while j<=2
sleep(1)
puts "func2 at: #{Time.now}"
j=j+1
end
end
puts "Started At #{Time.now}"
t1=Thread.new{func1()}
t2=Thread.new{func2()}
t1.join
t2.join
puts "End at #{Time.now}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment