开发者

Threads in Java

开发者 https://www.devze.com 2022-12-21 09:39 出处:网络
What is the difference between threads in java and native thr开发者_StackOverflow中文版eads?Java threads can be implemented in any way that conforms to the specification. The specification doesn\'t re

What is the difference between threads in java and native thr开发者_StackOverflow中文版eads?


Java threads can be implemented in any way that conforms to the specification. The specification doesn't require a specific implementation.

Effectively all modern desktop and/or server JVMs implement Java threads as native threads. That means that there is exactly 1 native thread for each Java thread and that the operating system does all the scheduling, just as it does for a C program, for example.

Some old JVMs and possibly some JVMs for devices with limited resources might implement threads in a way where the number of native threads used is smaller than the number of Java threads running (or possibly 1). Those implementations are said to implement so called "green threads". In this case the JVM itself is responsible for task switching and scheduling, as opposed to delegating that task to the operating system.


It depends on the implementation of the JVM, of course, but I think they are the same. It is, a Thread in Java is implemented via a native thread. You can expect/do with Java threads all kind of things you can with native threads.


Java threads and Native threads are completely different. Native thread is part of underlying platform (the OS).

Java threads are one of the feature of Java Language for supporting concurrency. Java specification controls API and functioning of Java threads. Ultimately Java threads will be mapped to native threads during execution of the java program. Also java threads needn't get one to one mapped with native threads.


Java Threads (Thread class and Runnable interface) are a much higher-level API than native threads in memory-shared applications. I recommended this book "Java Threads" by Oaks and Wong http://shop.oreilly.com/product/9780596007829.do. It's common practice to implement the Runnable interface, but it depends on your code scope.

0

精彩评论

暂无评论...
验证码 换一张
取 消