Skip to content

Instantly share code, notes, and snippets.

@vastus
Last active August 29, 2015 14:14
Show Gist options
  • Save vastus/fc4505d3db4363e2d852 to your computer and use it in GitHub Desktop.
Save vastus/fc4505d3db4363e2d852 to your computer and use it in GitHub Desktop.
Simple threading in Ruby
$ ruby threadit.rb
> 2
> 1
> 0
> 3
> 4
> 5
> 6
2 <
0 <
1 <
3 <
4 <
5 <
6 <
def costly(id)
print("> #{id}\n")
sleep 0.2
print(" #{id} <\n")
end
def run
7.times.map do |id|
Thread.new { costly(id) }
end.each(&:join)
end
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment