Discussions

Web tier: servlets, JSP, Web frameworks: what is green thread

  1. what is green thread (1 messages)

    dear friends ,

                 can any one tell me what is green threads and native threads,and explain it's functions ,and also tell me the role of dameon threads in java

    thanks
    rex(rexjonathan at rexsmail dot com)

    Threaded Messages (1)

  2. what is green thread[ Go to top ]

    I'll take a stab at your green threads question.

    Green threads is a term for the user-level threads
    package built into many JVMs.

    Not so long ago, some UNIX variants had kernels which
    didn't have a notion of multiple threads per process.
    One could still write multi-threaded programs by using
    a user-level threading library. Although programs
    which created and managed their threads using such a library worked, they had limitations. The threads were not at all visible to the operating system, and could not be scheduled separately. From the operating system's point of view, there was just a single process running.

    With user-level threads packages, you'd get strange situations where threads running in tight loops could never yield the processor; nothing preempted them unless you happened to make a call into the threads library (luckily, this wasn't too hard; most system calls resolved to the threads library, I think, and this gave the library a chance to switch threads if needed. Probably one reason why they always made you link the threads library in some particular order... to make various things resolve there instead of libc.so).

    Eventually, everyone added "kernel threads" to their
    operating systems. These were visible to the scheduler, and ran more efficiently.

    With kernel threads, the OS was aware of all the threads and could preempt them.

    I guess Java used a user-level threads package early on
    because it had to be available on so many different operating systems, some of which may not have had multi-thread-aware kernels.

    I remember Solaris JDKs giving you the option of using either green threads, or "native threads." The latter meant each Java thread was equivalent to a Solaris thread the OS could see and schedule.

    So I'd say use native threads whenever it's available,
    and use green threads only when you have to. This kind of thing is important on systems with multiple CPUs; I believe four native threads will run fine at once on a 4-CPU machine. With Green threads, that's not true; all the Java threads map to a single OS thread (which runs on a single CPU).