线程和队列 [英] Thread and Queue

查看:64
本文介绍了线程和队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣知道什么是实现基于线程的队列的最佳方法.

I am interested in knowing what would be the best way to implement a thread based queue.

例如:

我有10个动作,只想用4个线程执行.我想创建一个线性放置所有10个动作的队列,并用4个线程开始第一个4个动作,一旦一个线程完成执行,下一个便开始执行,等等-一次,线程数是4或小于4.

I have 10 actions which I want to execute with only 4 threads. I would like to create a queue with all the 10 actions placed linearly and start the first 4 action with 4 threads, once one of the thread is done executing, the next one will start etc - So at a time, the number of thread is either 4 or less than 4.

推荐答案

在标准库的thread中有一个Queue类.使用它,您可以执行以下操作:

There is a Queue class in thread in the standard library. Using that you can do something like this:

require 'thread'

queue = Queue.new
threads = []

# add work to the queue
queue << work_unit

4.times do
  threads << Thread.new do
    # loop until there are no more things to do
    until queue.empty?
      # pop with the non-blocking flag set, this raises
      # an exception if the queue is empty, in which case
      # work_unit will be set to nil
      work_unit = queue.pop(true) rescue nil
      if work_unit
        # do work
      end
    end
    # when there is no more work, the thread will stop
  end
end

# wait until all threads have completed processing
threads.each { |t| t.join }

我用非阻塞标志弹出的原因是在until queue.empty?和pop之间,另一个线程可能弹出了队列,因此,除非设置了非阻塞标志,否则我们将永远卡在该行

The reason I pop with the non-blocking flag is that between the until queue.empty? and the pop another thread may have pop'ed the queue, so unless the non-blocking flag is set we could get stuck at that line forever.

如果您使用MRI(默认的Ruby解释器),请记住线程不会绝对并发.如果您的工作受CPU限制,那么您也可以运行单线程.如果您有一些操作阻塞了IO,那么您可能会得到一些并行性,但是YMMV.另外,您可以使用允许完全并发的解释器,例如jRuby或Rubinius.

If you're using MRI, the default Ruby interpreter, bear in mind that threads will not be absolutely concurrent. If your work is CPU bound you may just as well run single threaded. If you have some operation that blocks on IO you may get some parallelism, but YMMV. Alternatively, you can use an interpreter that allows full concurrency, such as jRuby or Rubinius.

这篇关于线程和队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆