Scala 和 Erlang 使用绿色线程吗?
我读了很多关于 Scala 和 Erlang 如何实现轻量级线程及其并发模型(参与者)的内容。
但是,我有疑问。
Scala 和 Erlang 使用类似于 Java 使用的旧线程模型(绿色线程)的方法吗?
例如,假设有一台有 2 个核心的机器,那么 Scala/Erlang 环境将为每个处理器分叉一个线程?其他线程将由用户空间(Scala VM / Erlang VM)环境调度。这是正确的吗?
在幕后,这到底是如何运作的?
I've been reading a lot about how Scala and Erlang does lightweight threads and their concurrency model (actors).
However, I have my doubts.
Do Scala and Erlang use an approach similar to the old thread model used by Java (green threads) ?
For example, suppose that there is a machine with 2 cores, so the Scala/Erlang environment will fork one thread per processor? The other threads will be scheduled by user-space (Scala VM / Erlang VM ) environment. Is this correct?
Under the hood, how does this really work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Erlang 使用用户空间多任务处理,任务运行直到它们被阻塞或直到它们用完它们的“缩减”份额。约简被模糊地定义为计算单位。
在 SMP 调度程序出现之前,只有一个内核线程执行可运行的任务。通过 SMP 调度,您可以使用多个内核线程来执行任务,从而在多核计算机上并行执行代码。调度程序线程的数量应与核心数量匹配。请参阅 中的
-smp [enable|auto|disable]
开关erl 联机帮助页。还有一个内核线程池,供可加载驱动程序在其中执行阻塞系统调用。这称为异步线程池。请参阅erl 手册页中的
+A size
。进一步阅读
Erlang is using user-space multitasking, tasks run until they block or until they have used up their share of 'reductions'. A reduction is vaguely defined as a unit of computation.
Up until the SMP scheduler, there was just one kernel thread taking runnable tasks. With SMP scheduling you have several kernel threads taking tasks, and thus executing code in parallel on multi-core machines. The number of scheduler threads should match the number of cores. See the
-smp [enable|auto|disable]
switch in the erl manpage.There has also been a pool of kernel threads for loadable drivers to execute blocking system calls in. This is called the async thread pool. See
+A size
in the erl manpage.Further reading
Scala 2.8 使用 Java 线程池。轻量级 Actor (
Reactor
) 和轻量级模式下的较重 Actor (react {...}
) 不占用自己的线程;相反,当它们收到消息时,它们会占用一个线程,直到处理完该消息为止,然后返回该线程,并且在下一条消息到达之前根本不运行。这篇文章给出了一个不错的描述2.7 中的演员数量; 2.8 没那么不同。
Scala 2.8 uses Java thread pools. The lightweight actors (
Reactor
) and the heavier actors in lightweight mode (react {...}
) don't take their own thread; rather, when they have a message, they take a thread until they're done processing the message, then give back the thread and don't run at all until the next message hits.This article gives a decent description of Actors in 2.7; 2.8 is not that different.
有关 Erlang 实现详细信息的最新信息,请查看新鲜谈话 (幻灯片)。
For recent information about Erlang implementation details check fresh talk (slides).
Scala 使用底层 Java 线程实现,它使用本机线程。
不能说Erlang。
Scala uses the underlying Java thread implementation, which uses native threads.
Can't say about Erlang.