自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

喪心病狂,竟有Thread.sleep(0)這種神仙寫法?

開發(fā) 前端
Thread.sleep(0)?不是什么無用的代碼。sleep 方法可用于在 java 代碼中放置一個(gè)安全點(diǎn)??梢蕴崆霸陂L循環(huán)中觸發(fā)GC,避免GC線程長時(shí)間等待,從而避免達(dá)到拉長GC時(shí)間的目的。

?前言

最近在網(wǎng)上看到了一段代碼,讓我感到很迷茫。他在代碼中使用了Thread.sleep(0),讓線程休眠時(shí)間為0秒,具體代碼如下。

int i = 0;
while (i<10000000) {
// business logic

//prevent long time gc
if (i % 3000 == 0) {
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

sleep了0秒,不就是不睡覺嗎?我的第一反應(yīng)是這段代碼沒什么用,但是看到他的注釋又引起了我的興趣。經(jīng)過一番研究,看似無用的一段代碼,其實(shí)大有文章。

探索分析

為了找到原因,首先去看下sleep?方法的javadoc,如下:

Causes the currently executing thread to sleep (temporarily ceaseexecution) for the specified number of milliseconds, subject tothe precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.

顯然沒有得到正確的答案,最后在詢問作者說是使用Thread.sleep(0)可以暫時(shí)釋放CPU時(shí)間線。

時(shí)間片循環(huán)調(diào)度算法

在操作系統(tǒng)中,CPU有很多競爭策略。Unix系統(tǒng)采用時(shí)間片循環(huán)調(diào)度算法。在該算法中,所有進(jìn)程都被分組到一個(gè)隊(duì)列中。操作系統(tǒng)按順序?yàn)槊總€(gè)進(jìn)程分配一定的時(shí)間,即允許進(jìn)程運(yùn)行的時(shí)間。如果在時(shí)間片結(jié)束時(shí)進(jìn)程仍在運(yùn)行,則CPU將被剝奪并分配給另一個(gè)進(jìn)程,如果進(jìn)程在時(shí)間片內(nèi)阻塞或結(jié)束,則CPU立即切換。調(diào)度程序所要做的就是維護(hù)一個(gè)就緒進(jìn)程表。當(dāng)進(jìn)程用完時(shí)間片時(shí),它將被移到隊(duì)列的末尾。

上面的代碼中存在死循環(huán)。作者希望一直用一個(gè)線程來處理業(yè)務(wù)邏輯。如果Thread.sleep(0)?不使用主動(dòng)放棄CPU時(shí)間片,線程資源會(huì)一直被占用。眾所周知,GC 線程具有低優(yōu)先級(jí),因此Thread.sleep(0)?用于幫助 GC 線程嘗試競爭 CPU 時(shí)間片。但是為什么作者說可以防止long time GC呢?這就講到JVM的垃圾回收原理了。

GC的安全點(diǎn)

以HotSpot?虛擬機(jī)為例,JVM并不會(huì)在代碼指令流的任何位置暫停以啟動(dòng)垃圾回收,而是強(qiáng)制執(zhí)行必須到達(dá)安全點(diǎn)才暫停。換句話說,在到達(dá)安全點(diǎn)之前,JVM 不會(huì)為 GC STOP THE WORLD。

JVM 會(huì)在一些循環(huán)跳轉(zhuǎn)和方法調(diào)用上設(shè)置安全點(diǎn)。不過,為了避免安全點(diǎn)過多帶來的沉重負(fù)擔(dān),HotSpot虛擬機(jī)還有一個(gè)針對循環(huán)的優(yōu)化措施。如果循環(huán)次數(shù)少,執(zhí)行時(shí)間不宜過長。因此,默認(rèn)情況下不會(huì)將使用 int 或更小數(shù)據(jù)類型作為索引值的循環(huán)放置在安全點(diǎn)中。這種循環(huán)稱為可數(shù)循環(huán)。相應(yīng)地,使用long或更大范圍的數(shù)據(jù)類型作為索引值的循環(huán)稱為未計(jì)數(shù)循環(huán),將被放置在安全點(diǎn)。

但是,我們這里正好有一個(gè)可數(shù)循環(huán),所以我們的代碼不會(huì)放在安全點(diǎn)。因此,GC線程必須等到線程執(zhí)行完畢,才能執(zhí)行到最近的安全點(diǎn)。但如果使用Thread.sleep(0)?,則可以在代碼中放置一個(gè)安全點(diǎn)。我們可以看下HotSpot的safepoint.cpp源碼中的注釋,做除了說明。

// Begin the process of bringing the system to a safepoint.
// Java threads can be in several different states and are
// stopped by different mechanisms:
//
// 1. Running interpreted
// The interpeter dispatch table is changed to force it to
// check for a safepoint condition between bytecodes.
// 2. Running in native code
// When returning from the native code, a Java thread must check
// the safepoint _state to see if we must block. If the
// VM thread sees a Java thread in native, it does
// not wait for this thread to block. The order of the memory
// writes and reads of both the safepoint state and the Java
// threads state is critical. In order to guarantee that the
// memory writes are serialized with respect to each other,
// the VM thread issues a memory barrier instruction
// (on MP systems). In order to avoid the overhead of issuing
// a memory barrier for each Java thread making native calls, each Java
// thread performs a write to a single memory page after changing
// the thread state. The VM thread performs a sequence of
// mprotect OS calls which forces all previous writes from all
// Java threads to be serialized. This is done in the
// os::serialize_thread_states() call. This has proven to be
// much more efficient than executing a membar instruction
// on every call to native code.
// 3. Running compiled Code
// Compiled code reads a global (Safepoint Polling) page that
// is set to fault if we are trying to get to a safepoint.
// 4. Blocked
// A thread which is blocked will not be allowed to return from the
// block condition until the safepoint operation is complete.
// 5. In VM or Transitioning between states
// If a Java thread is currently running in the VM or transitioning
// between states, the safepointing code will wait for the thread to
// block itself when it attempts transitions to a new state.

可以看上面的第2點(diǎn) Running in native code?,而Thread.sleep(long millis)?是一種native方法。

總結(jié)

Thread.sleep(0)?不是什么無用的代碼。sleep 方法可用于在 java 代碼中放置一個(gè)安全點(diǎn)??梢蕴崆霸陂L循環(huán)中觸發(fā)GC,避免GC線程長時(shí)間等待,從而避免達(dá)到拉長GC時(shí)間的目的。

責(zé)任編輯:武曉燕 來源: JAVA旭陽
相關(guān)推薦

2022-08-29 10:52:37

線程函數(shù)操作系統(tǒng)

2024-11-18 17:06:11

Java線程

2020-04-26 14:40:19

戴爾

2018-04-10 12:04:25

程序員互聯(lián)網(wǎng)網(wǎng)絡(luò)

2022-05-24 12:57:49

函數(shù)代碼Java

2023-10-19 06:59:12

2023-03-01 16:26:20

馬斯克裁員

2009-06-27 11:34:32

2023-05-10 07:42:26

Java多線程編程

2016-10-11 08:53:38

Chrome瀏覽器Google

2014-02-04 08:18:51

2022-04-18 07:36:37

TimeUnit線程休眠

2020-08-20 07:38:51

Java字符串整形

2024-04-12 09:02:15

JavaCPU執(zhí)行時(shí)間線程

2017-10-24 13:42:55

流氓App安卓Google

2013-06-18 09:12:00

技術(shù)開發(fā)

2021-03-09 08:03:21

Node.js 線程JavaScript

2009-10-12 13:32:58

VB.NET線程構(gòu)造器

2013-06-18 09:28:12

程序員技術(shù)病

2015-12-08 11:30:01

WiFiWiFi技術(shù)過敏
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)