绿色线程与非绿色线程

时间:2021-02-18 21:06:14

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

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

  • On what environment Green Threads is better than Non Green ? Some say green threads are better for multi core processor.

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

  • Any Expected behaviour problems.

    任何预期的行为问题。

7 个解决方案

#1


51  

The Wikipedia article Green Threads explains it very well.

*的文章Green Threads解释得非常好。

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 this article. (It's about Solaris, but the fact that green threads are not used anymore is valid for the usual platforms).

特别是在Java的上下文中,绿色线程已经成为过去。看到这篇文章。 (这是关于Solaris的,但绿色线程不再使用的事实对于通常的平台是有效的)。

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.

自版本1.3发布以来,在Sun JVM for Linux中放弃了绿色线程(请参阅archive.org上的Linux平台上的Java [tm]技术)。这可以追溯到2000年。对于Solaris,可以从JDK 1.2获得本机线程。这可以追溯到1998年。我甚至认为Windows没有绿色线程实现,但我无法找到它的参考。

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

*文章中提到了一些例外,我主要针对低功耗(嵌入式)设备。

#2


14  

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.

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

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.

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

#3


14  

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.

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

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

换句话说,绿色线程在某种规模上更适合IO绑定操作。

#4


5  

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.

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

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

然后,Java删除了绿色线程,以便仅依赖本机线程。这使得Java 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.

请注意,我并没有特别谈论绿色线程的Java实现,因为它不像其他绿色线程影响在多核或多处理器系统中无法扩展。

#5


3  

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.

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

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.

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

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.

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

#6


2  

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.

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

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

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

#7


0  

JAVA Multi-Threading is implemented by two models:

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

  1. Green Thread Model
  2. 绿色线程模型
  3. Native OS Model
  4. 原生OS模型

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.

绿色线程模型:由JVM管理而不需要支持底层操作系统支持的线程称为绿色线程。很少有像Sun Solaris这样的操作系统提供对绿色线程模型的支持。它已弃用,不建议使用。

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.

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

#1


51  

The Wikipedia article Green Threads explains it very well.

*的文章Green Threads解释得非常好。

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 this article. (It's about Solaris, but the fact that green threads are not used anymore is valid for the usual platforms).

特别是在Java的上下文中,绿色线程已经成为过去。看到这篇文章。 (这是关于Solaris的,但绿色线程不再使用的事实对于通常的平台是有效的)。

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.

自版本1.3发布以来,在Sun JVM for Linux中放弃了绿色线程(请参阅archive.org上的Linux平台上的Java [tm]技术)。这可以追溯到2000年。对于Solaris,可以从JDK 1.2获得本机线程。这可以追溯到1998年。我甚至认为Windows没有绿色线程实现,但我无法找到它的参考。

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

*文章中提到了一些例外,我主要针对低功耗(嵌入式)设备。

#2


14  

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.

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

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.

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

#3


14  

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.

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

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

换句话说,绿色线程在某种规模上更适合IO绑定操作。

#4


5  

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.

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

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

然后,Java删除了绿色线程,以便仅依赖本机线程。这使得Java 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.

请注意,我并没有特别谈论绿色线程的Java实现,因为它不像其他绿色线程影响在多核或多处理器系统中无法扩展。

#5


3  

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.

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

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.

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

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.

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

#6


2  

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.

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

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

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

#7


0  

JAVA Multi-Threading is implemented by two models:

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

  1. Green Thread Model
  2. 绿色线程模型
  3. Native OS Model
  4. 原生OS模型

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.

绿色线程模型:由JVM管理而不需要支持底层操作系统支持的线程称为绿色线程。很少有像Sun Solaris这样的操作系统提供对绿色线程模型的支持。它已弃用,不建议使用。

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.

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