Last active
August 29, 2015 14:14
-
-
Save vastus/fc4505d3db4363e2d852 to your computer and use it in GitHub Desktop.
Simple threading in Ruby
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
$ ruby threadit.rb | |
> 2 | |
> 1 | |
> 0 | |
> 3 | |
> 4 | |
> 5 | |
> 6 | |
2 < | |
0 < | |
1 < | |
3 < | |
4 < | |
5 < | |
6 < |
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
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