ã¹ã¬ããä¸è¦§ã表示ããã
ããã°ã©ã çµäºæã«æ®åã¹ã¬ãããããã£ã½ãã®ã§èª¿æ»ãã¨ãããããã¹ã¬ããã®ä¸è¦§ã¯ããThread#listãã§åå¾ã§ããã
# ã¹ã¬ããä½æ (0..3).map {|i| Thread.start { loop { sleep 0.1 } } } # ã¹ã¬ããã®èµ·åãå¾ ã¤ sleep 0.5 # ã¹ã¬ããä¸è¦§ã表示 puts Thread.list
å®è¡çµæã§ãã
#<Thread:0x1042f8c8> #<Thread:0x1042f97c> #<Thread:0x1042fa30> #<Thread:0x1042fae4> #<Thread:0x1043de50>
ãã¼ããããã ã¨ã¹ã¬ãããã©ãã®èª°ã ããããããªã¼ãã¨ãããã¨ã§ãã¹ã¬ããã§ä½æå ã®ã¹ã¿ãã¯ãã¬ã¼ã¹ãè¦ããããã«ãã¦ã¿ããã¨ãã£ã¦ããã¹ã¬ããèµ·åæã«ãcallerãã®å¤ãåå¾ãã¦ã¹ã¬ããã«è¨å®ãã¦ããã ãã ãã©ã
clazz = class << Thread; self; end clazz.__send__(:alias_method, :start_org, :start ) def Thread.start(*args, &b) t = Thread.start_org( *args, &b) t[:stack] = caller # callerãã¹ã¬ããã«ç©ã return t end # ã¹ã¬ããä½æ (0..3).map {|i| Thread.start { loop { sleep 0.1 } } } # ã¹ã¬ããã®èµ·åãå¾ ã¤ sleep 0.5 # ã¹ã¬ããä¸è¦§ã表示 Thread.list.each {|t| puts "---#{t}" puts t[:stack] }
ã¡ããã¨ãããªãããThread#newãã¨ããThread#forkããä¸æ¸ãããªãã¨ã ããå®è¡çµæã¯ä»¥ä¸ã
---#<Thread:0x1042fc10> xxx/test.rb:27 xxx/test.rb:37:in `map' xxx/test.rb:26:in `each' xxx/test.rb:26:in `map' xxx/test.rb:26 ---#<Thread:0x1042fd78> xxx/test.rb:27 xxx/test.rb:37:in `map' xxx/test.rb:26:in `each' xxx/test.rb:26:in `map' xxx/test.rb:26 ---#<Thread:0x1042fee0> xxx/test.rb:27 xxx/test.rb:37:in `map' xxx/test.rb:26:in `each' xxx/test.rb:26:in `map' xxx/test.rb:26 ---#<Thread:0x10430048> xxx/test.rb:27 xxx/test.rb:37:in `map' xxx/test.rb:26:in `each' xxx/test.rb:26:in `map' xxx/test.rb:26 ---#<Thread:0x1043de50> nil
ããã§ä½æå ããããã¨ãã¹ã¬ãããã³ããã¼ã«ã¨ãæ¢ãã°ããããã ãã©ãã¾ãã¨ããããããã§èª¿æ»ã¯ã§ããããªã