2014-03-05 :-(
_ 午後
1300 テストしTARI
_ [Java][Ruby][マルチスレッド][デザインパターン]Java言語で学ぶデザインパターン入門マルチスレッド編 Thread-Specific Storage - スレッドごとのコインロッカー
スレッドごとの保存領域。
# coding: utf-8 # # 『増補改訂版Java言語で学ぶデザインパターン入門マルチスレッド編』 http://www.hyuki.com/dp/dp2.html # # Thread-Specific Storage - スレッドごとのコインロッカー # require 'thread' require 'monitor' class TSLog def initialize(filename) @writer = nil begin @writer = File.open(filename, "w") rescue => ex puts ex end end def println(s) @writer.puts(s) end def close() @writer.puts("==== End of log ====") @writer.close() end end class Log # ログを書く def self.println(name, s) getTSLog(name).println(s) end # ログを閉じる def self.close(name) getTSLog(name).close() end # スレッド固有のログを得る def self.getTSLog(name) tsLog = Thread.current.thread_variable_get(name) # そのスレッドからの呼び出しがはじめてなら、新規作成して登録する if tsLog == nil tsLog = TSLog.new("#{name}-log.txt") Thread.current.thread_variable_set(name, tsLog) end return tsLog end end class ClientThread < Thread def initialize(name) @name = name block = Proc.new { puts "#{@name} BEGIN" 0.upto(10) {|i| Log.println(@name, "i = #{i}") begin sleep(1) rescue => ex puts ex end } Log.close(@name) puts "#{@name} END" } super(&block) end end def main(argv) th ||= [] th << ClientThread.new("Alice") th << ClientThread.new("Bobby") th << ClientThread.new("Chris") th.each {|t| t.join() } end main(ARGV)
>ruby Thread-Specific-Storage.rb Alice BEGIN Bobby BEGIN Chris BEGIN Bobby END Chris END Alice 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
_ 買い物
iTune Store
- GARNiDELiA「ambiguous - EP」 キルラキル OP
とくさん、2007年の Twitter 忘年会[ 20071208#p04 ]で挨拶しただけだけどスンマセン