Ruby執行緒池
This is useful for a variety of scenarios. You create a thread pool, give it a maximum size, and pass a block to it everytime you need something processed. If all threads are busy, you block until a thread becomes free.
For example:
[code]pool = ThreadPool.new(10) # up to 10 threads
email_addresses.each do |addr|
pool.process {send_mail_to addr}
end[/code]
[code]require 'thread'
class ThreadPool
class Worker
def initialize
@mutex = Mutex.new
@thread = Thread.new do
while true
sleep 0.001
block = get_block
if block
block.call
reset_block
end
end
end
end
def get_block
@mutex.synchronize {@block}
end
def set_block(block)
@mutex.synchronize do
raise RuntimeError, "Thread already busy." if @block
@block = block
end
end
def reset_block
@mutex.synchronize {@block = nil}
end
def busy?
@mutex.synchronize {!@block.nil?}
end
end
attr_accessor :max_size
attr_reader :workers
def initialize(max_size = 10)
@max_size = max_size
@workers = []
@mutex = Mutex.new
end
def size
@mutex.synchronize {@workers.size}
end
def busy?
@mutex.synchronize {@workers.any? {|w| w.busy?}}
end
def join
sleep 0.01 while busy?
end
def process(&block)
wait_for_worker.set_block(block)
end
def wait_for_worker
while true
worker = find_available_worker
return worker if worker
sleep 0.01
end
end
def find_available_worker
@mutex.synchronize {free_worker || create_worker}
end
def free_worker
@workers.each {|w| return w unless w.busy?}; nil
end
def create_worker
return nil if @workers.size >= @max_size
worker = Worker.new
@workers << worker
worker
end
end[/code]
For example:
[code]pool = ThreadPool.new(10) # up to 10 threads
email_addresses.each do |addr|
pool.process {send_mail_to addr}
end[/code]
[code]require 'thread'
class ThreadPool
class Worker
def initialize
@mutex = Mutex.new
@thread = Thread.new do
while true
sleep 0.001
block = get_block
if block
block.call
reset_block
end
end
end
end
def get_block
@mutex.synchronize {@block}
end
def set_block(block)
@mutex.synchronize do
raise RuntimeError, "Thread already busy." if @block
@block = block
end
end
def reset_block
@mutex.synchronize {@block = nil}
end
def busy?
@mutex.synchronize {!@block.nil?}
end
end
attr_accessor :max_size
attr_reader :workers
def initialize(max_size = 10)
@max_size = max_size
@workers = []
@mutex = Mutex.new
end
def size
@mutex.synchronize {@workers.size}
end
def busy?
@mutex.synchronize {@workers.any? {|w| w.busy?}}
end
def join
sleep 0.01 while busy?
end
def process(&block)
wait_for_worker.set_block(block)
end
def wait_for_worker
while true
worker = find_available_worker
return worker if worker
sleep 0.01
end
end
def find_available_worker
@mutex.synchronize {free_worker || create_worker}
end
def free_worker
@workers.each {|w| return w unless w.busy?}; nil
end
def create_worker
return nil if @workers.size >= @max_size
worker = Worker.new
@workers << worker
worker
end
end[/code]
相關文章
- Java執行緒池二:執行緒池原理Java執行緒
- 淺談執行緒池(上):執行緒池的作用及CLR執行緒池執行緒
- 執行緒和執行緒池執行緒
- 多執行緒【執行緒池】執行緒
- 執行緒 執行緒池 Task執行緒
- 執行緒池執行緒
- 淺談執行緒池(中):獨立執行緒池的作用及IO執行緒池執行緒
- Java多執行緒——執行緒池Java執行緒
- java執行緒池趣味事:這不是執行緒池Java執行緒
- 執行緒池以及四種常見執行緒池執行緒
- java--執行緒池--建立執行緒池的幾種方式與執行緒池操作詳解Java執行緒
- java多執行緒9:執行緒池Java執行緒
- 二. 執行緒管理之執行緒池執行緒
- kuangshenshuo-多執行緒-執行緒池執行緒
- 執行緒的建立及執行緒池執行緒
- JavaThread多執行緒執行緒池Javathread執行緒
- Java多執行緒18:執行緒池Java執行緒
- 多執行緒之手撕執行緒池執行緒
- 執行緒池管理(1)-為什麼需要執行緒池執行緒
- 執行緒與執行緒池的那些事之執行緒池篇(萬字長文)執行緒
- 執行緒池 Executor執行緒
- Java執行緒池Java執行緒
- java 執行緒池Java執行緒
- 再聊執行緒池執行緒
- 執行緒池原理執行緒
- Android多執行緒之執行緒池Android執行緒
- Java多執行緒-執行緒池的使用Java執行緒
- 執行緒池建立執行緒的過程執行緒
- Java執行緒池一:執行緒基礎Java執行緒
- Android的執行緒和執行緒池Android執行緒
- 【Java】【多執行緒】執行緒池簡述Java執行緒
- java多執行緒系列之執行緒池Java執行緒
- golang workpool,工作池,執行緒池Golang執行緒
- 執行緒執行順序——CountDownLatch、CyclicBarrier 、join()、執行緒池執行緒CountDownLatch
- 執行緒池之ThreadPoolExecutor執行緒池原始碼分析筆記執行緒thread原始碼筆記
- 執行緒池之ScheduledThreadPoolExecutor執行緒池原始碼分析筆記執行緒thread原始碼筆記
- 原始碼|從序列執行緒封閉到物件池、執行緒池原始碼執行緒物件
- Java 執行緒池執行原理分析Java執行緒