- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我的问题是我不知道如何使用 Ruby 同步多个线程。任务是创建六个线程并立即启动它们。它们都应该按照我需要它工作的顺序一个接一个地做一些工作(例如 puts "Thread 1"Hi"
)。
经过一段时间与 Mutex 和 Condition Variable 的斗争之后,我实现了我的目标。这段代码有点乱,我故意没有使用循环来“看得更清楚”。
cv = ConditionVariable.new
mutex = Mutex.new
mutex2 = Mutex.new
cv2 = ConditionVariable.new
mutex3 = Mutex.new
cv3 = ConditionVariable.new
mutex4 = Mutex.new
cv4 = ConditionVariable.new
mutex5 = Mutex.new
cv5 = ConditionVariable.new
mutex6 = Mutex.new
cv6 = ConditionVariable.new
Thread.new do
mutex.synchronize {
puts 'First: Hi'
cv.wait(mutex)
puts 'First: Bye'
#cv.wait(mutex)
cv.signal
puts 'First: One more time'
}
end
Thread.new do
mutex.synchronize {
puts 'Second: Hi'
cv.signal
cv.wait(mutex)
puts 'Second:Bye'
cv.signal
}
mutex2.synchronize {
puts 'Second: Starting third'
cv2.signal
}
end
Thread.new do
mutex2.synchronize {
cv2.wait(mutex2)
puts 'Third: Hi'
}
mutex3.synchronize {
puts 'Third: Starting forth'
cv3.signal
}
end
Thread.new do
mutex3.synchronize {
cv3.wait(mutex3)
puts 'Forth: Hi'
}
mutex4.synchronize {
puts 'Forth: Starting fifth'
cv4.signal
}
end
Thread.new do
mutex4.synchronize {
cv4.wait(mutex4)
puts 'Fifth: Hi'
}
mutex5.synchronize {
puts 'Fifth: Starting sixth'
cv5.signal
}
end
Thread.new {
mutex5.synchronize {
cv5.wait(mutex5)
puts 'Sixth:Hi'
}
}
sleep 2
最佳答案
你可以滥用Queue ,像传统的一样使用它 PV Semaphore .为此,您创建一个 Queue 实例:
require 'thread'
...
sem = Queue.new
当一个线程需要等待时,它调用Queue#deq :
# waiting thread
sem.deq
当其他线程想要解除阻塞等待线程时,它会将某些东西(任何东西)推到队列中:
# another thread that wants to unblock the waiting thread
sem.enq :go
这是一个使用 Queue 来同步其开始和停止的工作类:
class Worker
def initialize(worker_number)
@start = Queue.new
Thread.new do
@start.deq
puts "Thread #{worker_number}"
@when_done.call
end
end
def start
@start.enq :start
end
def when_done(&block)
@when_done = block
end
end
构造时,一个工作线程创建一个线程,但该线程然后在 @start
队列上等待。直到调用#start,线程才会解除阻塞。
完成后,线程将执行调用#when_done 的 block 。稍后我们将看到如何使用它。
首先,让我们确保如果有任何线程引发异常,我们会发现它:
Thread.abort_on_exception = true
我们需要六个 worker :
workers = (1..6).map { |i| Worker.new(i) }
这里是#when_done 发挥作用的地方:
workers.each_cons(2) do |w1, w2|
w1.when_done { w2.start }
end
这需要每对 worker 轮流进行。每个 worker 除了最后一个被告知,当它完成时,它应该在它之后启动 worker 。那只剩下最后一个 worker 了。当它完成时,我们希望它通知这个线程:
all_done = Queue.new
workers.last.when_done { all_done.enq :done }
现在剩下的就是启动第一个线程:
workers.first.start
并等待最后一个线程完成:
all_done.deq
输出:
Thread 1
Thread 2
Thread 3
Thread 4
Thread 5
Thread 6
关于 ruby 同步 : How to make threads work one after another in proper order?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23766882/
我正在寻找通过 sql 查询对我的 sql 结果进行排序,大概在 order by 子句中使用某种嵌套的 order by/order by 我有以下数据: TERM USER I
我有一个表格,其中包含如下所示的部分数据。我已经在 edition_id 上完成了订购。现在还需要订购 laungauge_id,这取决于 edition_id 的值。 Edition_id 是指报纸
所以我有两个表,Questions 和 Answers,由多对多关系表 QuestionsAnswers 连接。 Questions 有一个排序列,允许我控制它们如何显示给用户,而 Questions
当我们说“高阶”函数时,我怀疑“阶”的真正含义是什么?例如,我有一个嵌入式函数调用: f.g.h 那么它叫“三阶”函数吗? “高阶”函数是静态函数累加的概念吗?然后当我有一个递归函数 f 时,在运行时
在具有多个 order by 子句的 SQL 查询中,它们是否真的在执行期间全部运行? 例子: select * from my_table order by field5, field3, fiel
我跟进 query其中 schema.org 数据库用于查找类的子级数量 - 作为比我的应用程序更简单的数据库。我想按字母顺序连接 child 的名字。查询: prefix schema: pre
正如 nazdrovje 所指出的(参见 here ) Ordering@Ordering 可用于获取列表中每个元素的排名。即使列表包含重复元素,结果也是 n 排列(作为整数 1 到 n 的有序列表,
我有两张 table 。 它们都有日期和 item_id 列。 我正在通过 item_id 加入他们。 结果应按两个日期列一起排序 下面的代码有效,生成正确的结果集... 但是它们仅按第一个表的日期排
尝试掌握 SQL 我想按日期订购,然后在其中按标题订购。 示例: SELECT * FROM tblboek ORDER BY jr_van_uitgave DESC 如何在按年龄的订单中按头衔排序?
我想使用 FIELD 参数对我的 SQL 输出进行排序,但是当我这样做时,它首先吐出我不想要的结果,然后它首先吐出我想要的结果。在结果之上,它首先吐出。如果这有意义的话 ;) 如何先吐出已定义的值,然
我有一个无法破解的排序问题。我这样从我的表中选择: SELECT * FROM 'sidemodules' WHERE name = 'module1' OR name = 'module2' OR
我对 Django oscar 的覆盖模型有疑问。我想为模型添加一个新字段,但是当我这样做时,我遇到了 RuntimeError: Conflicting 'order' models in appl
我有两个表,电影和类别,我想先按CategoryID获得一个排序列表,然后按名称排序。。电影表格有三个列ID、NAME和CategoryID。CATEGORY表有两列ID和NAME。。我尝试了下面这样
In a MySQL query, when using the DISTINCT option, does ORDER BY apply after the duplicates are re
我想创建一个 sql 查询,为 2 个不同的查询一起返回结果。例如,我想要以下形式的结果:产品名称, avg(price), min(price), max(price), avg(order), m
我正在使用行号从存储过程中获取分页结果。 我发现使用动态 case 语句列名称进行排序会减慢速度 - 但如果我对所有内容进行硬编码就可以了。 有没有办法通过不使整个 sql 查询一个字符串并使用 SP
如何在范围搜索中使用Morton Order? 在wiki中,在“使用一维数据结构进行范围搜索”段落中, 它说 "the range being queried (x = 2, ..., 3, y =
我正在使用 sequelize.js,我在使用 order 语句时遇到问题,我想先通过 if id 排序(如果我的 id 在该别名表中),然后再排序.... order = [['alias', 'i
我有一个 php 脚本,它从数据库中提取内容并以某种方式打印它们。数据库有一个名为“order”的列标题,它的 INT 大小为 11。当我从数据库中获取数据时,我试图按数据库中的值“order”对内容
我有一个带有 ORDER BY 子句的 UPDATE 查询。我已将相同的查询复制到具有相同 ORDER BY 子句的 SELECT 中,但得到了不同的结果。 更新查询: UPDATE t_locks
我是一名优秀的程序员,十分优秀!