Skip to content

Instantly share code, notes, and snippets.

@jwhiteman
Created November 19, 2016 19:22
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 jwhiteman/dad9c2ccb00988d7081d22c6620ec4aa to your computer and use it in GitHub Desktop.
Save jwhiteman/dad9c2ccb00988d7081d22c6620ec4aa to your computer and use it in GitHub Desktop.
Thread#join
# 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