java - 当所有JAVA线程都是使用OS库创建的 native 线程时,为什么要引入Fork/Join框架?

标签 java multithreading operating-system java-7

我所知道的是,在JDK 1.2之后,所有的Java线程都是使用“ native 线程模型”创建的,该模型在JNI和OS线程库的帮助下将每个Java线程与OS线程相关联。
因此,从下面的text中,我相信当今创建的所有Java线程都可以实现多核处理器的使用:

Multiple native threads can coexist. Therefore it is also called many-to-many model. Such characteristic of this model allows it to take complete advantage of multi-core processors and execute threads on separate individual cores concurrently.


但是当我在JAVA The Compelete Reference中阅读有关JDK 7中引入的Fork/Join Framework的介绍时:

Although the original concurrent API was impressive in its own right, it was significantly expanded by JDK 7. The most important addition was the Fork/Join Framework. The Fork/Join Framework facilitates the creation of programs that make use of multiple processors (such as those found in multicore systems). Thus, it streamlines the development of programs in which two or more pieces execute with true simultaneity (that is, true parallel execution), not just time-slicing.


这使我感到疑问,为什么自JDK 3起就存在“Java native 线程模型”,为什么引入了该框架?

最佳答案

Fork join框架不会替代原始的低级线程API;它使某些类别的问题更容易使用。
原始的低级线程API起作用:您可以使用系统上安装的所有CPU和CPU上的所有核心。如果您尝试实际编写多线程应用程序,您将很快意识到这很困难。
对于线程在很大程度上是独立的并且彼此之间不必共享信息的问题(即embarrassingly parallel problems),低级线程API的效果很好。但是,许多问题并非如此。使用低级API,很难以安全(产生正确的结果并且不会产生诸如死锁之类的不良影响)和有效(不浪费系统资源)的方式来实现复杂的算法。
Java fork/join框架(一种在fork/join model上的实现)被创建为一种高级机制,可简化对divide and conquer algorithms的并行计算应用。

关于java - 当所有JAVA线程都是使用OS库创建的 native 线程时,为什么要引入Fork/Join框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62981559/

相关文章:

linux - 子进程创建期间的调度

c - 不知道如何使用 tcsetpgrp()

java - 是否可以在离线模式下运行maven?

java - 在构造函数中保存到数据库

memory-management - 为什么操作系统没有垃圾收集器?

java - 无法预测所有线程何时完成执行

multithreading - 可重用屏障算法

java - getResource 将始终返回 null

java - 如何通过jsp页面中的超链接打开本地保存在Windows服务器上的pdf?

java - 划分每个线程在特定范围内使用ID