Class: Baykit::BayServer::Util::ExecutorService

Inherits:
Object
  • Object
show all
Defined in:
lib/baykit/bayserver/util/executor_service.rb

Defined Under Namespace

Classes: Executor

Constant Summary collapse

MAX_LEN_PER_EXECUTOR =
32

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, count) ⇒ ExecutorService

Returns a new instance of ExecutorService.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/baykit/bayserver/util/executor_service.rb', line 44

def initialize(name, count)
  @que = Thread::Queue.new
  @count = count
  @max_queue_len = MAX_LEN_PER_EXECUTOR * count
  @name = name

  count.times do |i|
    started = false
    Thread.new do
      started = true
      id = i + 1
      th_name = "Executor[#{name}]##{id}"
      Thread.current.name = th_name
      e = Executor.new @que, id, th_name
      e.run
    end
    while !started
      sleep(0.01)
    end
  end
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



40
41
42
# File 'lib/baykit/bayserver/util/executor_service.rb', line 40

def count
  @count
end

#max_queue_lenObject (readonly)

Returns the value of attribute max_queue_len.



41
42
43
# File 'lib/baykit/bayserver/util/executor_service.rb', line 41

def max_queue_len
  @max_queue_len
end

#nameObject (readonly)

Returns the value of attribute name.



42
43
44
# File 'lib/baykit/bayserver/util/executor_service.rb', line 42

def name
  @name
end

#queObject (readonly)

Returns the value of attribute que.



39
40
41
# File 'lib/baykit/bayserver/util/executor_service.rb', line 39

def que
  @que
end

Instance Method Details

#submit(tsk) ⇒ Object

post task



71
72
73
74
75
76
77
# File 'lib/baykit/bayserver/util/executor_service.rb', line 71

def submit(tsk)
  BayLog.debug("%s Submit: task=%s (qlen=%d/%d)", self, tsk, @que.length, @max_queue_len)
  if @que.length > @max_queue_len
    raise IOError("Task queue is full (>_<)")
  end
  @que.enq(tsk)
end

#to_sObject



66
67
68
# File 'lib/baykit/bayserver/util/executor_service.rb', line 66

def to_s()
  return "ExecutorService[#{@name}]"
end