我正在寻找如何在jruby中正确启动两个简单的线程。
在下面的两个线程中,第一个线程中的消息不会被打印出来。我可以交换这两个线程并且行为(第一个线程不打印)仍然存在:所以它不是关于线程的内容(我在任何情况下都是抄袭)
input_before_mysql_phases_t = Thread.new do
sprint "input_before_mysql_phases completed"
end
mysql_phases_t = Thread.new do
sprint "mysql_phases completed"
end
sprint "awaiting completion of data fetch phases .."
sleep 5
sprint "data fetch phases completed"
输出:注意第一个帖子的信息没有打印。
=> #<Thread:0x72d006a7 run>
[2013-04-18T12:56:29+00:00] SCHEDULER: mysql_phases completed
irb(main):540:0> sprint "awaiting completion of data fetch phases .."
[2013-04-18T12:56:29+00:00] SCHEDULER: awaiting completion of data fetch phases ..
=> nil
irb(main):541:0> sleep 5
=> 5
irb(main):542:0> sprint "data fetch phases completed"
[2013-04-18T12:56:34+00:00] SCHEDULER: data fetch phases completed
答案 0 :(得分:0)
什么是sprint
功能?
这有效:
input_before_mysql_phases_t = Thread.new do
sleep Random.rand(5) # Random sleep
sprintf "input_before_mysql_phases completed"
end
mysql_phases_t = Thread.new do
sleep Random.rand(5) #Random sleep
sprintf "mysql_phases completed"
end
puts "awaiting completion of data fetch phases .."
# Wait for threads to finish
input_before_mysql_phases_t.join
mysql_phases_t.join
puts "data fetch phases completed"