104

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.

2
  • 6
    It is extremely unlikely that green threads are better for multi-core processors as it will not use mroe than one core. If green threads are better then you have a program which shouldn't be using threads (sometimes threads are used when they shouldn't be) Apr 19, 2011 at 8:35
  • 1
    Where shouldn't threads be used? Mar 15, 2015 at 12:25

7 Answers 7

101

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.

12
  • 9
    A thing of the ancient past. Last century. Your professor needs to get up to date.
    – user207421
    Apr 19, 2011 at 8:13
  • 1
    @user1657170: what were they not? What "assisted threading" tech are you mentioning?
    – Mat
    Mar 15, 2015 at 12:48
  • 2
    @user1657170: still don't understand what you're referring to in what I wrote above. Read the article I linked to from ex-Sun, they abandoned green threads around Solaris 2.6.
    – Mat
    Mar 27, 2015 at 6:32
  • 4
    @user1657170: Fibres (and all the related/similar stuff that has popped up in pretty much all platforms these days, or have essentially been there forever as in Erlang processes) have nothing to do with green threads as the term was used in Java. Of course Sun/Oracle has native threads, and of course the JVM uses them. Again, if you have something specific to point out that is factually wrong in the above, please cite accurate sources.
    – Mat
    Jul 30, 2015 at 16:54
  • 3
    What do you call the three links in what I posted above, especially the link to Oracle docs that contradicts what you posted, without reference, on this question? I'm over answering your comments btw, this is a complete waste of time.
    – Mat
    Aug 2, 2015 at 15:07
27

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.

1
  • 21
    There are several implementations of green threads that use multiple native threads in order to utilize multi-core or multiprocessor architecture. This include the threading library in .NET and the Quasar library for Java. Both of them use as many native threads as there are cores and use green threads/fibers for additional threads, balancing them between the native threads. Aug 2, 2015 at 14:27
27

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.

4
  • 3
    With green threads, you don't really have a 100k concurrent connections. It's only an illusion.
    – Pacerier
    May 2, 2014 at 7:24
  • 11
    Technically, the connections would be concurrent, you just can't process their requests concurrently.
    – Yike Lu
    May 2, 2014 at 15:41
  • The location of the stack doesn't magically cause a speed up.
    – user207421
    Jul 15, 2018 at 21:24
  • How does green threads make the results of 100k concurrent connection appear on client screens? That's quite an illusion? Could we replicate that having 3d graphics as an illusion on the screen while the code do not produce it? Jun 6, 2020 at 14:20
5

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.

2
  • 2
    How are all green threads in the process blocked? What if this process has multiple kernel threads and an executor service to dispatch green threads on them? What if the thread doing the syscall simply doesn't get other tasks dispatched to it? Your claim is only true in a single-threaded environment.
    – marctrem
    May 17, 2017 at 5:02
  • 2
    What I meant is lets say there are 1 kernel thread for n green threads of a process and 1 green thread makes blocking call (which the single kernel thread handles) then all the green thread corresponding to that kernel thread will get blocked. Obviously this does not hold true for m kernel threads mapped to n green threads for a process. Either case you cannot say it is single threaded environment - depends on kernel thread scheduling. May 17, 2017 at 5:53
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.

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.

3
  • Green threads are not significantly faster, if faster at all, for compute intensive tasks. It is a great tool to have for certain tasks though.
    – JugsteR
    Jun 14, 2019 at 11:10
  • Yes they are. They're not faster at doing calculations but they are faster at threading. They do not offer parallelism like real treads do. Most cases of threading doesn't involve computer intense tasks. Although you might argue that people overuse threads and perhaps should use more state machines and fit what they do into procedural programming. Jun 6, 2020 at 7:55
  • There was no context given so please do not assume anything. Given you can run green threads on any number of cpu, just like "ordinary" threads, compute intensive in this case means that no switching will be going on. Since there is no switching, the benefit of using green threads for such purposes is defeated. I am sorry if that was unclear. So no, green threads in the particular scenario I was talking about, does not give you any performance benefits to speak of.
    – JugsteR
    Sep 16, 2020 at 9:48
3

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.

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.

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

1
  • 4
    There is no reason green threads can't utilise multiple CPUs. See, for example, GHC and Go.
    – user1804599
    Nov 22, 2015 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.