1. 程式人生 > >程序和程式關係類比/ java中執行緒是哪種實現【清華大學】作業系統

程序和程式關係類比/ java中執行緒是哪種實現【清華大學】作業系統

本文分三個小節

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