解决方法
它没有synchronized关键字,但是您可以通过Monitor类获得非常相似的东西.以下是编程
Ruby 1.8的一个例子:
require 'monitor' class Counter < Monitor attr_reader :count def initialize @count = 0 super end def tick synchronize do @count += 1 end end end c = Counter.new t1 = Thread.new { 100_000.times { c.tick } } t2 = Thread.new { 100_000.times { c.tick } } t1.join; t2.join c.count → 200000