What are Green Threads in java


11 points on green threads in java
I have seen many developers unaware of Green thread, it is very important topics from concept and interview point of view.


What are Green threads?
  1. Green threads are user-level threads. They are scheduled by an simple user-level process, not by the kernel.
  2. Green threads are implemented at the application level rather than in the OS level.
  3. Green threads are not at all dependent on OS thread scheduler.
  4. Green threads were used till Java 2, usage of Green threads was completely stopped in the java 3.
  5. Green threads are generally used when the OS does not provide multi-threading API’s.
  6. Green threads provides multi-threading capabilities on platforms that don't provide multi-threading capability.
  7. In green thread - JVM (java virtual machine) creates, manages, and context switches all threads within one OS (operating system) process. Operating system threads API’s are not used.

  1. Green Threads cannot take advantage of multi-core processors.

  1. Non-green threads have their own stack, but for Green threads memory is allocated from heap area

  1. They are name green thread because - Green was the code name given to project which provided JVM scheduled threads.


11 points - where we’ll discuss green threads in more detail -

1 -  Green threads were scheduled at which level?
Green threads are user-level threads. They are scheduled by an ordinary user-level process, not by the kernel.

2 - Green threads were implemented at which level - application or OS?
Green threads are implemented at the application level rather than in the OS (Operating system).

3 - Dependency on OS thread scheduler ?
Green threads are not at all dependent on OS thread scheduler.
As mentioned above Green threads are user-level threads/ user-scheduled processes so they don’t have to rely on OS thread scheduler.

4 - Green threads were used in which Java version?
Green threads were used till Java 2, usage of Green threads was completely stopped in the java 3. From Java 3, threads were implemented at OS(Operating system) level.

5 - When were Green threads generally used?
  • when the OS does not provide multi-threading API’s (or you can say Green threads can be used on platform that does not provide native multi-threading support),
  • when the OS does not provide complete set of multi-threading APIs,
  • when the multi-threading API’s doesn't work the way we need them to work for us.

6 - Memory allocation for Green threads. Stack or Heap?
  • Non-green threads have their own stack, but for Green threads memory is allocated from heap area.

7 - Can Green threads take advantage of multi-core processors? Why?
No, Green Threads cannot take advantage of multi-core processors.
Now, you must be wondering why green threads aren’t able to take advantage of multi-core processors. Answer is -
Because green threads is scheduled to simulate multi-threading environment but actually only green one thread exists, so single thread of course cannot be executed on multiple processors.
But in case of non-green threads multiple threads are used to create multi-threading environment, so multiple threads can be easily scheduled to take advantage of multi-core processors.

8 - Advantage of Green threads ?
Green threads provides multi-threading on platforms that don't provide multi-threading capability.
Or you can say Green threads can be used to simulate multi-threading on platforms that don't provide multi-threading capability.


9 - How to find out thread is green or not?
As usage of Green threads was completely stopped in the java 3. From Java 3, you will only find non-green threads.



10 - Does green threads use OS libraries?
In green thread - JVM (java virtual machine) creates, manages, and context switches all threads within one OS (operating system) process. Operating system threads API’s are not used.

In Non-green thread -
JVM creates and manages threads using OS (operating system) thread API’s.



11 - Why they are named green thread?
Green was the code name given to project which provided JVM scheduled threads.

No comments:

Post a Comment