作为开发人员,不同的Ruby线程模型(Ruby与JRuby)将对您的代码产生什么实际影响? [英] What practical effect will different Ruby threading models (Ruby vs JRuby) have on your code as a developer?

查看:65
本文介绍了作为开发人员,不同的Ruby线程模型(Ruby与JRuby)将对您的代码产生什么实际影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解MRI Ruby 1.8和JRuby之间不同线程模型的实际影响.

I'm trying to understand the practical impact of different threading models between MRI Ruby 1.8 and JRuby.

作为开发人员,这种差异对我意味着什么?

What does this difference mean to me as a developer?

而且,由于不同的线程模型,在MRI Ruby 1.8中是否有任何实用的代码示例在JRuby上具有较差的性能特征?

And also, are there any practical examples of code in MRI Ruby 1.8 that will have worse performance characteristics on JRuby due to different threading models?

推荐答案

状态

  • ruby​​ 1.8具有绿色线程,它们可以快速创建/删除(作为对象),但是并不能真正并行执行,甚至没有由操作系统而是由虚拟机调度
  • ruby​​ 1.9具有真实的线程,由于操作系统调用,它们创建/删除(作为对象)的速度很慢,但是由于GIL(全局解释器锁)一次只允许一个线程执行,所以它们都不是真正的平行
  • JRuby还具有操作系统安排的真实线程,并且是真正的并发
  • State

    • ruby 1.8 has green threads, these are fast to create/delete (as objects) but do not truly execute in parallel and are not even scheduled by the operating system but by the virtual machine
    • ruby 1.9 has real threads, these are slow to create/delete (as objects) because of OS calls, but because of the GIL (global interpreter lock) that only allows one thread to execute at a time, neither these are truly parallel
    • JRuby also has real threads scheduled by the OS, and are truly concurrent
    • 就线程角度而言,在2核CPU上运行的线程程序将比其他实现在JRuby上运行得更快

      A threaded program running on a 2-core CPU will run faster on JRuby then the other implementations, regarding the threading point of view

      许多现有的ruby库都不是线程安全的,因此JRuby的优势在很多时候都没有用.
      还应注意,如果要使用线程,则许多ruby编程技术(例如vars类)将需要付出额外的编程努力才能确保线程安全(互斥锁,监视器等).

      Many existing ruby libraries are not thread-safe so the advantage of JRuby in many times useless.
      Also note that many techniques of ruby programming (for example class vars) will need additional programming effort to ensure thread-safeness (mutex locks, monitors etc) if one is to use threads.

      这篇关于作为开发人员,不同的Ruby线程模型(Ruby与JRuby)将对您的代码产生什么实际影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆