Ruby スレッドを作成する場合、 の後Thread.newにブロックを指定すると、 new の後にすぐにスレッドが実行されます。また、Ruby はMonitorクラスをミューテックス ロックとして使用します。しかし、モニターオブジェクトをスレッド実行本体に渡す方法がわかりません。次のサンプル コードを参照してください。
thread1 = Thread.new do
sum=0
1.upto(10) {|x| sum = sum + x}
puts("The sum of the first 10 integers is #{sum}")
end
thread2 = Thread.new do
product=1
1.upto(10) {|x| product = product * x}
puts("The product of the first 10 integers is #{product}")
end
thread1.join
thread2.join
オブジェクトを新規作成し、Monitorそれを両方に渡しthread1、ステートメントthread2を同期させたい。putsどうやってするの?サンプルコードを教えてください。
この質問は、より一般的に尋ねられる可能性があります。オブジェクトをスレッド実行ブロックに渡す方法は?