class Noda::RQueue
ジョブのキュー実装 キューはスレッドセーフに書いている
キューから値を取り出すと、キューには残らない。
使用法¶ ↑
require 'noda' server =Noda::JobServer.new q = server.input #<= Jobサーバーが持ってる q.push Noda::MyTask.new("hogehgoe")
Attributes
name[R]
Public Class Methods
new( max=nil, name=nil )
click to toggle source
# File lib/noda/rqueue.rb, line 19 def initialize( max=nil, name=nil ) @name = name @list = [] @max = nil @max = max if max self.extend(MonitorMixin) @m_empty = self.new_cond @m_full = self.new_cond end
Public Instance Methods
_at(pos)
click to toggle source
キューの先頭N番目の値を調べる.チェック用。
-
pos 1からNまでの値を取る。先頭は1 で指定する.0始まりでないことに注意
# File lib/noda/rqueue.rb, line 73 def _at(pos) i = pos - 1 return @list.at(i) if (i) < self.size end
all()
click to toggle source
キューの値全てを取り出す.
# File lib/noda/rqueue.rb, line 70 def all() self.firsts(self.size) end
close_to_full?()
click to toggle source
キュー満杯が近いときにTrueを返す.
# File lib/noda/rqueue.rb, line 63 def close_to_full?() @list.size >= @max-5 end
empty?()
click to toggle source
キューに値があるか
# File lib/noda/rqueue.rb, line 61 def empty?() @list.empty? end
first()
click to toggle source
キューの先頭1個を取り出す. pop の別名
# File lib/noda/rqueue.rb, line 55 def first() self.pop end
firsts(n=1)
click to toggle source
キューの先頭N個を取り出す.
# File lib/noda/rqueue.rb, line 53 def firsts(n=1) (0...n).map{self.pop} end
full?()
click to toggle source
キューが満杯かどうか
# File lib/noda/rqueue.rb, line 65 def full?() # max=nil ならLimitless。つまり無限大 @list.size >= @max if @max end
include?(v)
click to toggle source
実験用メソッド・使わない.
# File lib/noda/rqueue.rb, line 50 def include?(v) @list.include? v end
Also aliased as: exists?
max_size()
click to toggle source
キュー格納可能数
# File lib/noda/rqueue.rb, line 59 def max_size() @max end
pop()
click to toggle source
キュー先頭からオブジェクトを取り出す.
キュー空なら実行スレッドをWaitさせる.
# File lib/noda/rqueue.rb, line 41 def pop self.synchronize{ @m_empty.wait_while{ self.empty? } obj = @list.shift @m_full.broadcast if @max obj } end
push(obj)
click to toggle source
キュー末尾にオブジェクトを追加。
キュー満杯時は実行スレッドをWaitさせます。
# File lib/noda/rqueue.rb, line 31 def push obj self.synchronize{ @m_full.wait_while{ self.full? } if @max @list.push obj @m_empty.broadcast } end
size()
click to toggle source
キューのサイズを取得
# File lib/noda/rqueue.rb, line 57 def size() @list.size end