进程和程序关系类比/ java中线程是哪种实现【清华大学】操作系统

版权声明:------------转载请标明链接.博客内容仅供参考,一切以官方文档为准!------------ https://blog.csdn.net/wabiaozia/article/details/84927439

本文分三个小节

1 线程模型

2 线程的实现 

3 java中线程是用户线程,内核线程,轻量级进程???

  • 3.1 临界区 互斥
  • 3.2 信号量 管程

前两小节是来自操作系统。

第三小节:看到操作系统中线程实现的三种方式,忽然想起我以前看到的一个问题,也很契合本文今天的主题,便加了进来。

1 线程进程

2 线程实现

(也有广义上分:除了内核线程其他都是用户线程。周志明深入理解java虚拟机12.4.1线程的实现)

3 java中线程是用户线程,内核线程,轻量级进程???

这个看到网上有不少用户谈到自己的理解:

内核线程,不然不能利用多核。。。;

和平台相关。。。;

混合模型。。。

到底哪种正确?

我这里摘录两篇博客。

1 R大

JVM中的线程模型是用户级的么? - RednaxelaFX的回答 - 知乎 https://www.zhihu.com/question/23096638/answer/29617153

作者:RednaxelaFX
链接:https://www.zhihu.com/question/23096638/answer/29617153
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
 

楼主的问题只能针对具体JVM实现来回答,在JVM规范里是没有规定的——具体实现用1:1(内核线程)、N:1(用户态线程)、M:N(混合)模型的任何一种都完全OK。Java并不暴露出不同线程模型的区别,上层应用是感知不到差异的(只是性能特性会不太一样…)

Java SE最常用的JVM是Oracle/Sun研发的HotSpot VM。在这个JVM的较新版本所支持的所有平台上,它都是使用1:1线程模型的——除了Solaris之外,它是个特例。

这是HotSpot VM在Solaris上所支持的线程模型:Java(TM) and Solaris(TM) Threading
<- 可以看到HotSpot VM在Solaris上支持M:N和1:1模型。当前默认是用1:1模型。

前面的回答里大家提到的“肯定不是用户态线程,不然怎么利用多核”、“多线程优势何在”,这些问题在使用N:1、M:N模型的JVM实现上确实存在。(菠萝注:使用用户态线程是会有潜在的并行瓶颈问题,但也有(一定程度的)解决办法,如下)

我喜欢用的一个例子是Oracle/Sun的另一个JVM实现,用于Java ME CLDC的CLDC HotSpot Implementation(CLDC-HI)。它支持两种线程模型,默认使用N:1线程模型,所有Java线程都映射到一个内核线程上,是典型的用户态线程模型;它也可以使用一种特殊的混合模型,Java线程仍然全部映射到一个内核线程上,但当Java线程要执行一个阻塞调用时,CLDC-HI会为该调用单独开一个内核线程,并且调度执行其它Java线程,等到那个阻塞调用完成之后再重新调度之前的Java线程继续执行。
有权限访问到的同学可以去读读CLDC HotSpot Implementation Architecture Guide(百度文库有对应CLDC-HI 2.0的版本:CLDC-Hotspot-Architecture_百度文库),里面的第5章介绍了CLDC-HI的线程模型,下面引用部分内容过来:

The system has two distinct threading models. The simplest and preferred model supports LWTs (light weight threads). In this model, CLDC HotSpot Implementation implements all LWTs on a single native OS thread. LWTs are essentially co-routines created and scheduled by the virtual machine. This is transparent at the Java runtime environment level.
...
A special style of handling threading might be preferable in some ports. This style relies on the availability of native thread support in the target platform OS. It is called hybrid threading, ...

然后在另一份文档,CLDC HotSpot Implementation Porting Guide的第4章里介绍了如何移植CLDC-HI的线程系统到别的平台。http://elastos.org有一份CLDC-HI 1.1.3版本的:http://elastos.org/elorg_files/FreeBooks/java/thesis/CLDC-Hotspot-Port.pdf。同样摘抄一小段描述出来:

Non-blocking scheduling - The native method de-schedules its lightweight
thread (LWT) until another part of the virtual machine determines that the native
method can be executed without blocking. Then the native method is reentered to
proceed with the given problematic call that is now guaranteed to be of
sufficiently short duration.
Hybrid threading - The native method de-schedules its LWT after delegating the
task to an OS thread to execute the given blocking call. When this OS thread,
which is truly concurrent to the rest of the virtual machine, completes the call, it
causes the LWT to resume. The LWT then reenters the native method to fetch the
results of the blocking call.
...
If you port to another platform, it might be the case that only one of the styles can be
implemented. Non-blocking scheduling depends on the existence of functions that
can determine whether a subsequent call would block. Take for example the
select() function for BSD sockets that can be used to determine whether a socket
is ready for a non-blocking data transmission call. Hybrid threading requires that
several OS threads are available and that all the blocking OS calls that you want to
employ are reentrant.


可见,使用用户态线程是会有潜在的并行瓶颈问题,但也有(一定程度的)解决办法。

2 周志明 深入理解java虚拟机

1.2前使用用户线程实现,1.2后替换为基于操作系统的原生线程模型实现。

windows和linux下:1对1线程模型,一条java线程映射到一条轻量级进程。因为windows和linux系统提供的模型是一对一的。

solaris:通过虚拟机参数使得一对一和多对多可以。

转载请标明连接:https://blog.csdn.net/wabiaozia/article/details/84927439

3.1 临界区 互斥

方法3:更高级的抽象(有test-and-set或交换之一便可以简洁的设计出临界区的进入或退出操作)

3.2 信号量 管程

10.2 信号量

10.5管程抽象度比信号量还要高

linux 信号量是什么怎么用? - 灵剑的回答 - 知乎 https://www.zhihu.com/question/47411729/answer/105848845

临界区,互斥,互斥量,信号量,管程? - 胖君的回答 - 知乎 https://www.zhihu.com/question/39850927/answer/242109380

猜你喜欢

转载自blog.csdn.net/wabiaozia/article/details/84927439