Green Threads


The first point we will talk is about native threads, which happens in OS level. An example is POSIX threads. This mainly means that file and memory are shared among all threads.  If you use C to program on Linux you can link pthreads.

In computer science, green threads are threads that are scheduled by Virtual Machine, therefore in JAVA this is made by JVM. Green threads are essentially simulated threads. The Java Virtual Machine would take care of switching between different green threads, but the JVM itself would only run in on OS Thread. This can bring for us some advantages:

  • If we are using native threads, the overhead is so increased. It is also usually slower to switch between native threads than using green threads.
  • A system can usually support a much higher number of green threads than OS Threads.
  • It would be practical to spawn a new green thread for every new connection on a web server, but it is not generally practical to spawn a new native thread for every incoming HTTP connection.

There are disadvantages:

There is only one native thread, so its is  the only thread that gets scheduled. Even if there are multiple CPUs and multiple green threads, only one CPU will be running one green thread at any time. It mains that green threads cannot take advantage of multiprocessor system. Since JVM creates are not threads at OS Level.JVM splits its timeslice among the various threads that it spawns internally. Thus, the JVM threads are bound to run within that single JVM thread that runs inside a single processor.

As a result, native threads can have benefits from multiple processors. There are time when you might to use Green Threads. You must to know what time you can use these specifics threads.