Why doesn't Ruby have a ThreadPool built-in?

送分小仙女□ 提交于 2019-12-20 10:25:22

问题


I have a program that creates 10000 threads at once, and runs 8 at the same time.

But ruby doesn't have a ThreadPool built-in as Java. Is there a good reason?


回答1:


Most likely the reason is because ruby doesn't have "real" threads. It has what are called Green threads. The ruby interpreter takes care of scheduling execution threads without using any underlying OS threads. This effectively makes Ruby single threaded.




回答2:


probably because it's easy to roll your own using the standard library "Queue" class.

q = Queue.new
3.times { Thread.new {  while something = q.pop(true) rescue nil; ... }

It's a good question though--I might suggest bringing it up with Ruby Core.




回答3:


My suspicion would be it's because a ThreadPool wouldn't be that useful in C-based implementations of Ruby. You can use only one processor at a time with Matz's Ruby Intepreter or Yet Another Ruby VM.

If you want multiple threads to be run on multiple processors, you need to use JRuby instead.



来源:https://stackoverflow.com/questions/3840751/why-doesnt-ruby-have-a-threadpool-built-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!