Intro Counted Loop Versus Uncounted Loop

Intro Counted Loop Versus Uncounted Loop

這篇介紹Counted Loop Versus Uncounted Loop。

Counted Loop

1
2
JVM認為比較短的for loop,不會使用Safe Point。
ex:用int當作index的for loop

Uncounted Loop

1
2
3
4
5
6
JVM會在每次Loop return之前放置Safe Point,ex:用long當作index的for loop。
所以一個常見的問題是當某個thread跑進了Counted loop時,JVM啟動了GC,
需要等待這個thread把Loop跑完才能進入Safe Point,如果這個loop是個大loop,
且loop內執行的比較慢,而且不存在其他函數調用產生其它Safe Point,
這時就需要等待該thread跑完loop才能從其他位置進入Safepoint,
這就導致了其它thread可能會長時間的Stop-the-world。