Green and native threads -- what's the difference?

Question: What is the difference between green threads and native threads?

Answer: Green threads, the threads provided by the JVM, run at the user level, meaning that the JVM creates and schedules the threads itself. Therefore, the operating system kernel doesn't create or schedule them. Instead, the underlying OS sees the JVM only as one thread.

Green threads prove inefficient for a number of reasons. Foremost, green threads cannot take advantage of a multiprocessor system. Since the JVM simply runs from within one thread, the threads that the JVM creates are not threads at the OS level. Instead, the 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.

In contrast, native threads are created and scheduled by the underlying operating system. Rather than create and schedule threads itself, the JVM creates the threads by calling OS-level APIs. As a result, native threads can benefit from multiple processors. Performance can improve because an IO-blocked thread will no longer block the entire JVM. The block will only block the thread waiting for the IO resource.

There are times when you might want to run green threads. First, native threads are not always available. Second, if your code is not thread-safe, you may experience errors when running native threads.

(from http://www.javaworld.com/javaworld/javaqa/2001-04/02-qa-0413-four.html?page=1)

 

Version 1.1 is based on green threads and won't be covered here. Green threads are simulated threads within the VM and were used prior to going to a native OS threading model in 1.2 and beyond. Green threads may have had an advantage on Linux at one point (since you don't have to spawn a process for each native thread), but VM technology has advanced significantly since version 1.1 and any benefit green threads had in the past is erased by the performance increases over the years.

(from http://java.sun.com/docs/hotspot/threads/threads.html)

 

in green threads model only one operating system thread is used by java
virtual machine for all java threads you use in your programs. in native
threads model, for every thread you create in java you get one separate
operating system thread.

as i know, on windows nt, only native threads exist while on solaris
platform, exist both models. on older JVM on solaris only green threads
model existed.

(from http://www.velocityreviews.com/forums/t130194-green-threads-in-java.html)

發佈了5 篇原創文章 · 獲贊 4 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章