Ruby synchronized

最近学习 Java 的时候了解到在 Javasynchronized 关键字可以保证代码块的串行执行。

ruby 开发中往往使用第三方来保证,比如使用数据库或文件系统。其实 ruby 也有类似的方式来保证代码块的串行执行,它就是 Mutexsynchronize

require 'thread'
semaphore = Mutex.new

a = Thread.new {
  semaphore.synchronize {
    # access shared resource
  }
}

b = Thread.new {
  semaphore.synchronize {
    # access shared resource
  }
}

上一篇
Ruby thread pool Ruby thread pool
有时候会遇到一个情况需要在多个远程服务获取数据。 例如服务 a 需要 2 秒,服务 b 需要 5 秒,按照正常处理总共就需要 2+5 总共 7 秒了。 如果 a 和 b 之间并没有相互依赖关系,我们可以使用 thread pool 来并发获
2015-04-10
下一篇
Rails Singular Resources Rails Singular Resources
Rails 中 Controller 如果不是复数,写 path 或 url 的时候就必须加上 index,这样看上去就很 low。 resources :photo do collection do get 'search
2014-10-24