英文:
How is the deprecated Green Threads different from Project Loom?
问题
在早期的Java版本中,JVM线程复用了本地操作系统线程。这些线程被称为绿色线程。这在Java的早期版本中已经被弃用,然后每个Java线程对应一个操作系统线程。
然而,通过项目Loom,执行上下文不再是一个线程,而是一个可以执行代码的“某个”对象。但是它仍然需要复用一个本地线程来执行,对吗?如果是这样的话,对我来说这看起来像是回到了绿色线程的状态。
我相当确定我漏掉了一些东西,我想知道那是什么。能否请你给我一些帮助?
英文:
In the very early Java versions, JVM threads multiplexed to native OS threads. Such threads were called Green Threads. This was deprecated in the very early versions of Java and then every Java thread corresponded to an OS thread.
However, with Project Loom - where the execution context is no longer a Thread but "some" object that can execute code. But it must still multiplex to a native thread for execution, shouldn't it? If that is the case, to me this looks like going back to the Green threads.
I'm pretty sure I'm missing something that I'd like to know what it is. Can I please get some help on this?
答案1
得分: 3
绿色线程是在根本不支持本地多线程的系统上支持多线程的解决方案。在这种系统中,单个阻塞调用会阻塞整个进程,因此所有线程都会受到影响。此外,在绿色线程中无法利用SMP功能。
虽然在概念上相关,但在没有多线程支持的系统上的n:1线程和具有多线程支持的系统上的n:m线程在代码上看起来完全不同。
这在项目Loom的虚拟线程上更为真实,因为它可以在Java侧使用当时不存在的线程池实现。因此,虚拟线程功能的很大一部分已在Java中实现。
虚拟线程仍然可以使用SMP,因为它们的执行被委托给适应CPU核心数量的平台线程池。当平台线程即将被阻塞时,可以启动新的平台线程,以保持并行处理能力。但实现者还努力重新实现了阻塞操作,使其在底层成为非阻塞操作,因此平台线程将只将虚拟线程置于阻塞或等待状态,并继续执行下一个可运行的虚拟线程。
当然,可以一直将Java的多线程发展成这个方向(使其看起来像是对绿色线程的扩展)。规范始终允许将Java线程映射到平台线程的n:m映射。但这是一条未走的道路。如果那时尝试,结果很可能会与项目Loom完全不同。
英文:
Green Threads were a solution to support multi-threading on systems with no native multi-threading support at all. In such a system, a single blocking call would block the entire process, hence all threads. Further, there was no way to use SMP capabilities with Green Threads.
While conceptually related, n:1 threads on a system without multi-threading support and n:m threads on a system with multi-threading support would have looked totally different code-wise.
This is even more true with the virtual threads of project Loom as it could settle on Java side thread pool implementations which did not exist back then. So a large portion of the Virtual Threads feature has been implemented in Java.
Virtual threads can still use SMP, as their execution is delegated to pool of platform threads tailored to the number of CPU cores. A new platform thread can be started when a platform thread is about to get blocked, to keep up the parallel processing capability. But the implementers also spent effort on re-implementing blocking operations to be non-blocking under the hood, so the platform thread will only put the virtual thread into blocked or wait state and proceed to execute the next runnable virtual thread.
Of course, it would have been possible to develop Java’s multi-threading into this direction all the time (so it would have looked like an extension to Green Threads). The specification always allowed n:m mappings of Java threads to platform threads. But it was a road not taken. And if attempted back then, the result would most probably look totally different to project Loom.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论