I'm trying to understand the practical impact of different threading models between MRI Ruby 1.8 and JRuby.
我试图了解MRI Ruby 1.8和JRuby之间不同线程模型的实际影响。
What does this difference mean to me as a developer?
作为开发人员,这对我来说意味着什么?
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?
而且,由于不同的线程模型,MRI Ruby 1.8中的代码是否有任何实际的JRuby性能特征?
3 个解决方案
#1
11
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
ruby 1.8有绿色线程,这些线程可以快速创建/删除(作为对象),但不能真正并行执行,甚至不是由操作系统调度,而是由虚拟机调度
ruby 1.9有真正的线程,由于OS调用,这些创建/删除(作为对象)很慢,但由于GIL(全局解释器锁)只允许一次执行一个线程,所以这些都不是真正的并行
JRuby还有操作系统安排的真实线程,并且是真正的并发
Conclusion
A threaded program running on a 2-core CPU will run faster on JRuby then the other implementations, regarding the threading point of view
在线程上,在2核CPU上运行的线程程序在JRuby和其他实现上运行得更快
Notice!
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库都不是线程安全的,因此JRuby的优势在很多时候都是无用的。另请注意,许多ruby编程技术(例如类变量)将需要额外的编程工作来确保线程安全性(互斥锁,监视器等),如果要使用线程。
#2
6
JRuby's threads are native system threads, so they give you all the benefits of threaded programming (including the use of multiple processor cores, if applicable). However, Ruby has a Global Interpreter Lock (GIL), which prevents multiple threads from running simultaneously. So the only real performance difference is the fact that your MRI/YARV Ruby applications won't be able to utilize all of your processor cores, but your JRuby applications will happily do so.
JRuby的线程是本机系统线程,因此它们为您提供线程编程的所有好处(包括使用多个处理器内核,如果适用)。但是,Ruby有一个全局解释器锁(GIL),它可以防止多个线程同时运行。所以唯一真正的性能差异是你的MRI / YARV Ruby应用程序无法利用你所有的处理器内核,但你的JRuby应用程序很乐意这样做。
However, if that isn't an issue, MRI's threads are (theoretically, I haven't tested this) a little faster because they are green threads, which use fewer system resources. YARV (Ruby 1.9) uses native system threads.
但是,如果这不是问题,那么MRI的线程(理论上,我没有测试过这个)的速度要快一些,因为它们是绿色线程,它使用较少的系统资源。 YARV(Ruby 1.9)使用本机系统线程。
#3
3
I am a regular JRuby user and the biggest difference is that JRuby threads are truly concurrent. They are actually system level threads so they can be executed concurrently on multiple cores. I do not know of any place where MRI Ruby 1.8 code runs slower on JRuby. You might consider checking out this question Does ruby have real multithreading?.
我是一个常规的JRuby用户,最大的区别是JRuby线程是真正的并发。它们实际上是系统级线程,因此可以在多个内核上并发执行。我不知道在JRuby上MRI Ruby 1.8代码运行速度较慢的地方。您可以考虑查看这个问题ruby是否具有真正的多线程?
#1
11
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
ruby 1.8有绿色线程,这些线程可以快速创建/删除(作为对象),但不能真正并行执行,甚至不是由操作系统调度,而是由虚拟机调度
ruby 1.9有真正的线程,由于OS调用,这些创建/删除(作为对象)很慢,但由于GIL(全局解释器锁)只允许一次执行一个线程,所以这些都不是真正的并行
JRuby还有操作系统安排的真实线程,并且是真正的并发
Conclusion
A threaded program running on a 2-core CPU will run faster on JRuby then the other implementations, regarding the threading point of view
在线程上,在2核CPU上运行的线程程序在JRuby和其他实现上运行得更快
Notice!
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库都不是线程安全的,因此JRuby的优势在很多时候都是无用的。另请注意,许多ruby编程技术(例如类变量)将需要额外的编程工作来确保线程安全性(互斥锁,监视器等),如果要使用线程。
#2
6
JRuby's threads are native system threads, so they give you all the benefits of threaded programming (including the use of multiple processor cores, if applicable). However, Ruby has a Global Interpreter Lock (GIL), which prevents multiple threads from running simultaneously. So the only real performance difference is the fact that your MRI/YARV Ruby applications won't be able to utilize all of your processor cores, but your JRuby applications will happily do so.
JRuby的线程是本机系统线程,因此它们为您提供线程编程的所有好处(包括使用多个处理器内核,如果适用)。但是,Ruby有一个全局解释器锁(GIL),它可以防止多个线程同时运行。所以唯一真正的性能差异是你的MRI / YARV Ruby应用程序无法利用你所有的处理器内核,但你的JRuby应用程序很乐意这样做。
However, if that isn't an issue, MRI's threads are (theoretically, I haven't tested this) a little faster because they are green threads, which use fewer system resources. YARV (Ruby 1.9) uses native system threads.
但是,如果这不是问题,那么MRI的线程(理论上,我没有测试过这个)的速度要快一些,因为它们是绿色线程,它使用较少的系统资源。 YARV(Ruby 1.9)使用本机系统线程。
#3
3
I am a regular JRuby user and the biggest difference is that JRuby threads are truly concurrent. They are actually system level threads so they can be executed concurrently on multiple cores. I do not know of any place where MRI Ruby 1.8 code runs slower on JRuby. You might consider checking out this question Does ruby have real multithreading?.
我是一个常规的JRuby用户,最大的区别是JRuby线程是真正的并发。它们实际上是系统级线程,因此可以在多个内核上并发执行。我不知道在JRuby上MRI Ruby 1.8代码运行速度较慢的地方。您可以考虑查看这个问题ruby是否具有真正的多线程?