2014-02-21 :-)
_ 私の熱い独身活動 ドクカツ! 始まります! フフッヒ
飯を作る気力がないのでインスタントにするなど怠惰なセイカツをしている。
_ ,
ツッコミ spam 増えたなあ。ksg
_ [Java][Ruby][マルチスレッド][デザインパターン]Java言語で学ぶデザインパターン入門マルチスレッド編 Thread-Per-Message - この仕事、やっといてね
メッセージごとにスレッドを起動して処理する。
(起動済スレッドへメッセージを投げるのは Worker-Thread パターン)
# coding: utf-8 # # 『増補改訂版Java言語で学ぶデザインパターン入門マルチスレッド編』 http://www.hyuki.com/dp/dp2.html # # Thread-Per-Message - この仕事、やっといてね # require 'thread' require 'monitor' class Host def initialize() @helper = Helper.new() end def request(count, c) puts " request(#{count}, #{c}) BEGIN" t = Thread.new() { @helper.handle(count, c) } puts " request(#{count}, #{c}) END" return t end end class Helper def handle(count, c) puts " handle(#{count}, #{c}) BEGIN" 0.upto(count) {|i| slowly() printf c } puts "" puts " handle(#{count}, #{c}) END" end def slowly() begin sleep(2) rescue => ex puts ex end end end def main(argv) th ||= [] puts "main BEGIN" host = Host.new(); th << host.request(10, 'A') th << host.request(20, 'B') th << host.request(30, 'C') th.each {|t| t.join()} puts "main END" end main(ARGV)
>ruby Thread-Per-Message.rb main BEGIN request(10, A) BEGIN request(10, A) END request(20, B) BEGIN request(20, B) END request(30, C) BEGIN request(30, C) END handle(10, A) BEGIN handle(20, B) BEGIN handle(30, C) BEGIN ACBCABCABCABCBAACBBCACABCBACABCBA handle(10, A) END BCCBBCCBCBCBBCBCBCB handle(20, B) END CCCCCCCCCCC handle(30, C) END main END
ライセンス
Copyright (C) 2002,2006 Hiroshi Yuki. http://www.hyuki.com/dp/dp2.html hyuki@hyuki.com This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.
参考
『増補改訂版Java言語で学ぶデザインパターン入門マルチスレッド編』
4797331623
[ツッコミを入れる]