Created
June 29, 2010 22:40
-
-
Save bhuga/457935 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class ThreadTest | |
| def initialize | |
| # kicks off the thread, and only afterwards assigns thread to @t | |
| @t = Thread.new { joiner } | |
| end | |
| def joiner | |
| # without this sleep, will sometimes crash, | |
| # as @t is not yet the thread, so will try to call @t.join on nil | |
| # sleep 0.5 | |
| if @t == Thread.current | |
| puts "I'm the same thread!" | |
| else | |
| @t.join | |
| puts "that's not me, I'm joining!" | |
| end | |
| end | |
| end | |
| test = ThreadTest.new | |
| result = test.joiner | |
| # output with sleep at line 11: | |
| # I'm the same thread! | |
| # that's not me, I'm joining! | |
| # | |
| # output without sleep at line 11 is sometimes the above, and sometimes the following: | |
| # test-threads.rb:15:in `joiner': undefined method `join' for nil:NilClass (NoMethodError) | |
| # | |
| # Happens in 1.8.7, 1.9.1, JRuby |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment