绿色线程与非绿色线程

发布于 2024-11-02 05:33:14 字数 118 浏览 1 评论 0原文

我想了解这些类型的线程提供的优势。

  • 在什么环境下绿色线程比非绿色线程更好?有人说绿色线程更适合多核处理器。

  • 任何预期的行为问题。

I'd like to understand the advantages provided by these type of threads.

  • In what environments are green threads better than non-green? Some say green threads are better for multi core processors.

  • Any expected behaviour problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

神妖 2024-11-09 05:33:14

维基百科文章 Green Threads 对此进行了很好的解释。

绿色线程是“用户级线程”。它们是由“普通”用户级进程调度的,而不是由内核调度的。因此它们可用于在不提供该功能的平台上模拟多线程。

特别是在 Java 环境中,绿色线程已经成为过去。请参阅文章适用于 Solaris 的 JDK 1.1 开发人员指南。 (这是关于 Solaris 的,但不再使用绿色线程的事实对于通常的平台是有效的)。

自版本 1.3 发布以来,绿色线程在适用于 Linux 的 Sun JVM 中已被放弃(请参阅 Linux 平台上的 Java[tm] 技术,位于 archive.org)。这可以追溯到 2000 年。对于 Solaris,从 JDK 1.2 开始就可以使用本机线程。这可以追溯到 1998 年。我什至不认为 Windows 曾经有过绿色线程实现,但我找不到相关参考。

正如维基百科文章中所述,有一些例外情况,我收集的主要针对低功耗(嵌入式)设备。

The Wikipedia article Green Threads explains it very well.

Green threads are "user-level threads". They are scheduled by an "ordinary" user-level process, not by the kernel. So they can be used to simulate multi-threading on platforms that don't provide that capability.

In the context of Java specifically, green threads are a thing of the past. See article JDK 1.1 for Solaris Developer's Guide. (It's about Solaris, but the fact that green threads are not used anymore is valid for the usual platforms).

Green threads were abandoned in the Sun JVM for Linux as of the release of version 1.3 (see Java[tm] Technology on the Linux Platform on archive.org). That dates back to 2000. For Solaris, native threads were available from JDK 1.2. That dates back to 1998. I don't even think there ever was a green thread implementation for Windows, but I can't find a reference for that.

There are some exceptions as noted in the Wikipedia article, I gather mostly for low-power (embedded) devices.

十二 2024-11-09 05:33:14

绿色线程内存是从堆中分配的,而不是由操作系统为其创建堆栈。这可能会使并发线程数量增加一个数量级或更多。正如其他人提到的,这不会自动利用多个处理器,但用例通常用于阻塞 I/O——例如,绿色线程可能允许您处理 100k 并发连接,而​​不是 10k。

换句话说,绿色线程更适合一定规模的 IO 绑定操作。

Green thread memory is allocated from the heap rather than having a stack created for it by the OS. This can potentially give an order of magnitude or more increase in concurrent threads. As other people have mentioned, this would not take advantage of multiple processors automatically, however the use case is typically for blocking I/O -- for example green threads might allow you to handle 100k concurrent connections as opposed to 10k.

So in other words, green threads are better for IO bound operations at a certain scale.

冬天的雪花 2024-11-09 05:33:14

绿色线程是在应用程序级别而不是操作系统中实现的线程。这通常是在操作系统不提供线程 API 或它不能按您需要的方式工作时完成的。

因此,优点是您可以获得类似线程的功能。缺点是绿色线程实际上不能使用多个核心。

有一些早期的 JVM 使用绿色线程(IIRC Blackdown JVM 移植到 Linux 就是这样做的),但现在所有主流 JVM 都使用真正的线程。可能有一些嵌入式 JVM 仍然使用绿色线程。

Green threads are threads implemented at the application level rather than in the OS. This is usually done when the OS does not provide a thread API, or it doesn't work the way you need.

Thus, the advantage is that you get thread-like functionality at all. The disadvantage is that green threads can't actually use multiple cores.

There were a few early JVMs that used green threads (IIRC the Blackdown JVM port to Linux did), but nowadays all mainstream JVMs use real threads. There may be some embedded JVMs that still use green threads.

淡写薰衣草的香 2024-11-09 05:33:14

绿色线程是用户级线程而不是内核级线程。它们是由用户库而不是内核来调度的。您可以拥有自己的调度机制来调度线程,而不是依赖操作系统调度程序。

绿色线程模拟多线程环境,不依赖任何本机操作系统功能,并且它们在用户空间而不是内核空间中进行管理,使它们能够在没有本机线程支持的环境中工作

性能:

在多核处理器上,本机线程实现可以自动将工作分配给多个处理器,而绿色线程实现通常不能。
绿色线程在线程激活和同步方面明显优于 Linux 本地线程。

当绿色线程执行阻塞系统调用时,不仅该线程被阻塞,而且进程中的所有线程都被阻塞。

Green threads are user level threads rather than kernel level threads. They are scheduled by user libraries rather than the kernel. You can have your own scheduling mechanism to schedule threads rather than relying on the OS scheduler.

Green threads emulate multithreaded environments without relying on any native OS capabilities, and they are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread support

Performace :

On a multi-core processor, native thread implementations can automatically assign work to multiple processors, whereas green thread implementations normally cannot.
Green threads significantly outperform Linux native threads on thread activation and synchronization.

When a green thread executes a blocking system call, not only is that thread blocked, but all of the threads within the process are blocked.

不弃不离 2024-11-09 05:33:14

当活动线程多于处理器时,绿色线程比本机线程快得多。

Java 最初支持绿色线程,但与大多数现代绿色线程实现不同,它无法在多个处理器上扩展,从而使 Java 无法利用多个内核。

然后,Java 删除了绿色线程,以便仅依赖本机线程。这使得 Java 线程比绿色线程慢。

请注意,我并不是专门谈论绿色线程的 Java 实现,它确实有缺点,因为它与其他绿色线程含义不同,无法在多核或多处理器系统中扩展。

Green threads are significantly faster than native threads when having more active threads than processors.

Java initially had support for green threads but unlike most modern green threading implementations it could not scale over multiple processors, making Java unable to utilise multiple cores.

Then Java removed green threading in order to rely only on native threads. That made Java Threads slower than green threads.

Please notice that I am not specifically talking about the Java implementation of green threads which did have disadvantages as it unlike other green thread implications could not scale in a multicore or multiprocessor system.

裂开嘴轻声笑有多痛 2024-11-09 05:33:14

JAVA多线程由两种模型实现:

  1. 绿色线程模型
  2. Native OS模型

绿色线程模型:由JVM管理,不接受底层OS支持的线程称为绿色线程线。很少有操作系统(例如 Sun Solaris)提供对绿色线程模型的支持。它已被弃用,不建议使用。

本机操作系统模型:在底层操作系统的帮助下由 JVM 管理的线程称为本机操作系统模型。所有 Windows 操作系统都提供对本机操作系统模型的支持。

JAVA Multi-Threading is implemented by two models:

  1. Green Thread Model
  2. Native OS Model

Green Thread Model: The Thread which is managed by JVM, without taking underlying OS support is called Green Thread. Very few OS like Sun Solaris provide support for green thread model. It is deprecated and not recommended to use.

Native OS Model: The Thread which is manged by the JVM with the help of underlying OS is called Native OS Model. All windows OS provide support for native OS model.

月依秋水 2024-11-09 05:33:14

绿色线程不是由操作系统调度的。

这意味着它们的调度发生在用户空间中,而不是由内核处理。这意味着绿色线程通常无法使用所有 CPU 核心。

对于当今运行 Java 的任何主流平台(例如 x86 或 x64),您将使用真正的线程。

Green threads aren't scheduled by the OS.

That means that the scheduling for them happens in userspace and is not handled by the kernel. This means that the green threads can't usually be made to use all the CPU cores.

For any mainstream platform running Java these days (eg x86 or x64), you'll be using real threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文