Ruby 线程 class 练习
Ruby threads class exercise
我正在做一些练习以了解 Ruby 上线程的使用。似乎有些概念我没有遇到我正在尝试做的 class 问题。
在一个市场上,一位农民收获了一种产品并将其放在一个展位上。一名员工拿起产品并将其带到客户的柜台。使两个Ruby线程从参展商处放入和取出产品。
这是我的解决方案,但它不起作用:
require 'thread'
mutex = Mutex.new
cv = ConditionVariable.new
orchard = ["Letuce", "Tomato", "Potato"]
exhibitor = []
counter = []
farmer = Thread.new do
mutex.synchronize do
puts "Harvesting..."
product = huerta.pop()
puts "Placing the product in an exhibitor..."
exhibitor.push(product)
cv.signal
end
end
employee = Thread.new do
mutex.synchronize do
cv.wait(mutex)
puts "Taking product from exhibitor..."
product = exhibitor.pop()
puts "Placing product in the counter..."
counter.push(product)
end
end
employee.join
farmer.join
代码背后的想法是,两个线程 运行 同时,当 farmer 线程结束时发送一个信号,该信号被员工线程截获,等待它做这件事。
我得到以下输出:
Harvesting...
Placing the product in an exhibitor...
Traceback (most recent call last):
1: from pruebahilos.rb:38:in `<main>'
pruebahilos.rb:38:in `join': No live threads left. Deadlock? (fatal)
2 threads, 2 sleeps current:0x0000557b0cf8ca50 main thread:0x0000557b0ce98050
* #<Thread:0x0000557b0cec6d78 sleep_forever>
rb_thread_t:0x0000557b0ce98050 native:0x00007fadd7b13ec0 int:0
pruebahilos.rb:38:in `join'
pruebahilos.rb:38:in `<main>'
* #<Thread:0x0000557b0d0b5850 pruebahilos.rb:24 sleep_forever>
rb_thread_t:0x0000557b0cf8ca50 native:0x00007fadd4324700 int:0
depended by: tb_thread_id:0x0000557b0ce98050
pruebahilos.rb:26:in `sleep'
pruebahilos.rb:26:in `wait'
pruebahilos.rb:26:in `block (2 levels) in <main>'
pruebahilos.rb:25:in `synchronize'
pruebahilos.rb:25:in `block in <main>'
发生错误是因为当您创建员工线程并对其调用 join 时,农民的线程可能已经完成其工作并退出(这就是“没有活动线程”错误试图告诉我们的)。
尝试更改创建线程的顺序(在农民线程之前创建员工线程)- 这样应该可以。
我正在做一些练习以了解 Ruby 上线程的使用。似乎有些概念我没有遇到我正在尝试做的 class 问题。
在一个市场上,一位农民收获了一种产品并将其放在一个展位上。一名员工拿起产品并将其带到客户的柜台。使两个Ruby线程从参展商处放入和取出产品。
这是我的解决方案,但它不起作用:
require 'thread'
mutex = Mutex.new
cv = ConditionVariable.new
orchard = ["Letuce", "Tomato", "Potato"]
exhibitor = []
counter = []
farmer = Thread.new do
mutex.synchronize do
puts "Harvesting..."
product = huerta.pop()
puts "Placing the product in an exhibitor..."
exhibitor.push(product)
cv.signal
end
end
employee = Thread.new do
mutex.synchronize do
cv.wait(mutex)
puts "Taking product from exhibitor..."
product = exhibitor.pop()
puts "Placing product in the counter..."
counter.push(product)
end
end
employee.join
farmer.join
代码背后的想法是,两个线程 运行 同时,当 farmer 线程结束时发送一个信号,该信号被员工线程截获,等待它做这件事。
我得到以下输出:
Harvesting...
Placing the product in an exhibitor...
Traceback (most recent call last):
1: from pruebahilos.rb:38:in `<main>'
pruebahilos.rb:38:in `join': No live threads left. Deadlock? (fatal)
2 threads, 2 sleeps current:0x0000557b0cf8ca50 main thread:0x0000557b0ce98050
* #<Thread:0x0000557b0cec6d78 sleep_forever>
rb_thread_t:0x0000557b0ce98050 native:0x00007fadd7b13ec0 int:0
pruebahilos.rb:38:in `join'
pruebahilos.rb:38:in `<main>'
* #<Thread:0x0000557b0d0b5850 pruebahilos.rb:24 sleep_forever>
rb_thread_t:0x0000557b0cf8ca50 native:0x00007fadd4324700 int:0
depended by: tb_thread_id:0x0000557b0ce98050
pruebahilos.rb:26:in `sleep'
pruebahilos.rb:26:in `wait'
pruebahilos.rb:26:in `block (2 levels) in <main>'
pruebahilos.rb:25:in `synchronize'
pruebahilos.rb:25:in `block in <main>'
发生错误是因为当您创建员工线程并对其调用 join 时,农民的线程可能已经完成其工作并退出(这就是“没有活动线程”错误试图告诉我们的)。
尝试更改创建线程的顺序(在农民线程之前创建员工线程)- 这样应该可以。