Created
November 19, 2016 19:22
-
-
Save jwhiteman/dad9c2ccb00988d7081d22c6620ec4aa to your computer and use it in GitHub Desktop.
Thread#join
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
# A reminder that join conceptually combines the stacks of the parent and child threads. | |
# If you consider that the stack grows downard, the child stack will sit below the parent | |
# stack. See output below. | |
def main? | |
Thread.current == Thread.main | |
end | |
def name | |
if main? | |
"main" | |
else | |
"thread" | |
end | |
end | |
def shout(msg) | |
puts "#{name}: #{msg}" | |
end | |
def a | |
b | |
shout "done with a" | |
end | |
def b | |
c | |
shout "done with b" | |
end | |
def c | |
if main? | |
Thread.new do | |
sleep 3 | |
a | |
shout "done with thread" | |
end.join | |
end | |
shout "done with c" | |
end | |
a | |
=begin | |
thread: done with c | |
thread: done with b | |
thread: done with a | |
thread: done with thread | |
main: done with c | |
main: done with b | |
main: done with a | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment