1
A
回答
1
從Ruby documentation for Queue#pop:
從隊列中檢索數據。如果隊列爲空,則將調用線程掛起,直到將數據推入隊列。如果non_block爲true,則該線程不會掛起,並引發異常。
您正在使用單線程,因此您的隊列永遠不會包含任何對象,因此您的線程會永久掛起(死鎖)。
試試這個
require "thread"
queue = Queue.new
thread1 = Thread.new do
5.times do |i|
x = queue.pop
sleep rand(i) # simulate workload
puts "taken #{x} from queue!"
end
end
thread2 = Thread.new do
5.times do |i|
sleep rand(i) # simulate workload
queue.push i
puts "pushed #{i} to the queue!"
end
end
thread1.join
你有兩個線程,因此您將不會遇到死鎖。當隊列爲空時,消費者線程將被掛起,但當第二個線程將某些東西推送到隊列時,它將再次變爲活動狀態。
相關問題
- 1. 紅寶石散列在紅寶石
- 2. 寶石安裝紅寶石
- 3. 調試寶石紅寶石
- 4. Twitter的紅寶石寶石
- 5. 紅寶石寶石 - LoadError
- 6. 錯誤紅寶石寶石
- 7. 卸載紅寶石寶石
- 8. 紅寶石寶石文檔
- 9. 評估紅寶石
- 10. $ DEBUG紅寶石全局變量
- 11. 在軌道上的紅寶石屈服於新的紅寶石佈局
- 12. 在紅寶石
- 13. 在紅寶石
- 14. 紅寶石
- 15. 紅寶石
- 16. 紅寶石
- 17. 紅寶石
- 18. 紅寶石
- 19. 在紅寶石
- 20. 在紅寶石
- 21. 從紅寶石
- 22. 紅寶石file.foreach
- 23. OS紅寶石
- 24. 紅寶石
- 25. 紅寶石
- 26. 與紅寶石
- 27. date_select紅寶石
- 28. 紅寶石
- 29. 紅寶石
- 30. 與紅寶石