2014-02-18 :-(
_ 午後
1300 デバッグしTARI
_ [Java][Ruby][マルチスレッド][デザインパターン]Java言語で学ぶデザインパターン入門マルチスレッド編 Producer-Consumer - わたしが作り、あなたが使う
生産者と消費者
供給と需要
# coding: utf-8
#
# 『増補改訂版Java言語で学ぶデザインパターン入門マルチスレッド編』 http://www.hyuki.com/dp/dp2.html
# 
# Producer-Consumer - わたしが作り、あなたが使う
#
require 'thread'
require 'monitor'
class Table
  def initialize(count)
    @buffer = Array.new(count)
    @head = 0
    @tail = 0
    @count = 0
    @lock = Monitor.new()
    @cond = @lock.new_cond()
  end
  
  def put(cake)
    @lock.synchronize {
      puts "#{Thread.current} puts #{cake}"
      while @count >= @buffer.length
        @cond.wait()
      end
      
      @buffer[@tail] = cake
      @tail = (@tail + 1) % @buffer.length
      @count += 1
      @cond.broadcast()
    }
  end
  
  def take()
    @lock.synchronize {
      while @count <= 0
        @cond.wait()
      end
      
      cake = @buffer[@head]
      @head = (@head + 1) % @buffer.length
      @count -= 1
      @cond.broadcast()
      puts "#{Thread.current} takes #{cake}"
      return cake
    }
  end
end
class EaterThread < Thread
  def initialize(name, table, seed)
    @table = table
    @random = Random.new(seed)
    
    block = Proc.new {
      begin
        cake = table.take()
        sleep(@random.rand(5))
      rescue => ex
        puts ex
      end
    }
    super(&block)
  end
end
class MakerThread < Thread
  def initialize(name, table, seed)
    @random = Random.new(seed)
    @table = table
    @lock = Monitor.new
    @@id = 0  # ケーキの通し番号(コックさん全員共通)
    
    block = Proc.new {
      begin
        sleep(@random.rand(5))
        cake = "[ Cake No. #{nextid()} by #{Thread.current} ]"
        table.put(cake)
      rescue => ex
        puts ex
      end
    }
    
    super(&block)
  end
  def nextid()
    @lock.synchronize {
      @@id += 1
      return @@id
    }
  end
end
def main(argv)
  th ||= []
  table = Table.new(3)
  th << MakerThread.new("MakerThread-1", table, 31415)
  th << MakerThread.new("MakerThread-2", table, 92653)
  th << MakerThread.new("MakerThread-3", table, 58979)
  th << EaterThread.new("EaterThread-1", table, 32384)
  th << EaterThread.new("EaterThread-2", table, 62643)
  th << EaterThread.new("EaterThread-3", table, 38327)
  th.each {|t| t.join() }
end
main(ARGV)
ruby Producer-Consumer.rb #<MakerThread:0x0000000046bd38> puts [ Cake No. 1 by #<MakerThread:0x0000000046bd38> ] #<EaterThread:0x0000000046b630> takes [ Cake No. 1 by #<MakerThread:0x0000000046bd38> ] #<MakerThread:0x0000000046b978> puts [ Cake No. 2 by #<MakerThread:0x0000000046b978> ] #<EaterThread:0x0000000046b248> takes [ Cake No. 2 by #<MakerThread:0x0000000046b978> ] #<MakerThread:0x00000002c4c758> puts [ Cake No. 3 by #<MakerThread:0x00000002c4c758> ] #<EaterThread:0x0000000046b4a0> takes [ Cake No. 3 by #<MakerThread:0x00000002c4c758> ]
ライセンス
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
[ツッコミを入れる]








