トップ «前の日記(2014-02-14) 最新 次の日記(2014-02-16)» 編集

ヨタの日々

2001|08|09|10|11|12|
2002|01|02|03|04|05|06|07|08|09|10|11|12|
2003|01|02|03|04|05|06|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|11|12|
2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|12|
2024|01|02|03|04|

2014-02-15 :-)

_ 午前

0900 起床 && 部屋掃除

1010 おひる。うどん

1030 アニメ消化

_ 午後

1200 買い物

1500 アニメ消化

IMG_3868

_

1700 ぐったり。部屋の那珂が温まらないのでオフトゥン.in

2100 飯。アジの開き

2200 おやつ。クラシックショコラ( ref. ちゃんと作れるスイーツ pp.26-27 ) 最近作ってなかった。

IMG_3878

_ [Java][Ruby][マルチスレッド][デザインパターン]Java言語で学ぶデザインパターン入門マルチスレッド編 Guarded Suspension - 用意できるまで、待っててね

リクエストが put されるまで get はブロックする。待機する。

スピンロックなどとも言う。

# coding: utf-8

#
# 『増補改訂版Java言語で学ぶデザインパターン入門マルチスレッド編』 http://www.hyuki.com/dp/dp2.html
# 
# Guarded Suspension - 用意できるまで、待っててね
#

require 'thread'
require 'monitor'

class Request
  def initialize(name)
    @name = name
  end
  
  def getName()
    return @name
  end
  
  def to_s()
    return "[ Request #{@name} ]"
  end
end

class RequestQueue
  def initialize()
    @lock = Monitor.new()
    @cond = @lock.new_cond
    @queue ||= []
  end
  
  def getRequest()
    @lock.synchronize {
      while @queue.empty? == true
        begin
          @cond.wait()
        rescue
        end
      end
      return @queue.pop()
    }
  end
  
  def putRequest(request)
    @lock.synchronize {
      @queue.push(request)
      @cond.broadcast()
    }
  end
end

class ServerThread < Thread
  def initialize(requestQueue, name, seed)
    @requestQueeu = requestQueue
    @random = Random.new(seed)
    
    block = Proc.new {
      0.upto(10) {|i|
        request = requestQueue.getRequest()
        puts "#{self} handles #{request}"
        begin
          sleep(random.rand(10))
        rescue
        end
      }
    }
    super(&block)
  end
end

class ClientThread < Thread
  def initialize(requestQueue, name, seed)
    @requestQueue = requestQueue
    @random = Random.new(seed)
    
    block = Proc.new {
      0.upto(10) {|i|
        request = Request.new("No. #{i}")
        puts "#{self} requests #{request}"
        @requestQueue.putRequest(request)
        begin
          sleep(random.rand(10))
        rescue
        end
      }
    }
    super(&block)
  end
end

def main(argv)
  th ||= []
  requestQueue = RequestQueue.new()
  th << ServerThread.new(requestQueue, "Bobby", 6535897)
  th << ClientThread.new(requestQueue, "Alice", 3141592)
  th.each {|t| t.join() }
end

main(ARGV)
ruby GuardedSuspension.rb
#<ClientThread:0x000000002fbde0> requests [ Request No. 0 ]
#<ClientThread:0x000000002fbde0> requests [ Request No. 1 ]
#<ServerThread:0x00000002abd608> handles [ Request No. 0 ]
#<ClientThread:0x000000002fbde0> requests [ Request No. 2 ]
#<ServerThread:0x00000002abd608> handles [ Request No. 1 ]
#<ClientThread:0x000000002fbde0> requests [ Request No. 3 ]
#<ServerThread:0x00000002abd608> handles [ Request No. 2 ]
#<ClientThread:0x000000002fbde0> requests [ Request No. 4 ]
#<ServerThread:0x00000002abd608> handles [ Request No. 3 ]
#<ClientThread:0x000000002fbde0> requests [ Request No. 5 ]
#<ServerThread:0x00000002abd608> handles [ Request No. 4 ]
#<ClientThread:0x000000002fbde0> requests [ Request No. 6 ]
#<ServerThread:0x00000002abd608> handles [ Request No. 5 ]
#<ClientThread:0x000000002fbde0> requests [ Request No. 7 ]
#<ServerThread:0x00000002abd608> handles [ Request No. 6 ]
#<ClientThread:0x000000002fbde0> requests [ Request No. 8 ]
#<ServerThread:0x00000002abd608> handles [ Request No. 7 ]
#<ClientThread:0x000000002fbde0> requests [ Request No. 9 ]
#<ServerThread:0x00000002abd608> handles [ Request No. 8 ]
#<ClientThread:0x000000002fbde0> requests [ Request No. 10 ]
#<ServerThread:0x00000002abd608> handles [ Request No. 9 ]
#<ServerThread:0x00000002abd608> handles [ Request No. 10 ]

ライセンス

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