1. 程式人生 > >System類詳解-2 exit gc runFinalization identityHashCode

System類詳解-2 exit gc runFinalization identityHashCode

System類詳解

0.說明 :這篇寫System類還沒有分析完的問題 ,主要解決一下幾個問題

  • exit(int) 方法
  • gc方法
  • runFinalization方法
  • identityHashCode
1. exit方法  ----等價於Runtime.getRuntime().exit(int status) 終止當前執行的jvm。方法引數是一個狀態碼,非0代表不正常的終止,the method never returns normally(關於這句話的詳細解釋參見:https://stackoverflow.com/questions/3715967/when-should-we-call-system-exit-in-java)----我個人理解,當執行緒執行到這個語句的時候,會終止jvm,執行緒並不會返回繼續往下執行程式碼。 在終止jvm的時候,會初始化它的shutdown sequence(shutdown佇列)。shutdown sequence包含兩部分,第一部分是執行所有已經註冊的shutdownhook ,這些shutdownhook會並行的執行,並且在執行期間,後臺程序(dameon thread)也在執行,第二部分是,如果啟用了finalization-on-exit(呼叫System.runFinalizersOnExit),那麼會執行所有沒有被呼叫的finalizers,一旦這個工作完成,jvm就會停止。

--------------------接下來深入分析一下exit方法--------------- 廢話不說,上圖
可以看出,起決定性作用的是Shutdown.exit方法,接下來我們分析這個方法---------------------------------不在這分析了令起一篇blog分析,Runtime,ApplicationShutdown ,Shutdown之間的關係 2.gc   等價於Runtime.getRuntime().gc   呼叫這種方法表明,Java虛擬機器花費大量的工作來回收未使用的物件,以便使它們當前佔用的記憶體可供快速重用。 當控制從方法呼叫返回時,虛擬機器已盡最大努力回收所有丟棄的物件

3.runFinalization   ------會有一片部落格詳細介紹,區分runFinalization和gc方法的區別 4.identityHashCode

Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode(). The hash code for the null reference is zero.

-----解釋:不管類有沒有重寫Object的hashCode方法,identityHashCode都返回通過預設的hashCode產生的值,也就是說identityHashCode的結果和Object類中的hashCode的結果是一樣的-------換句話說,不管你的類是否有重寫Object類中的hashCode方法,identityHashCode始終返回和Object中的hashCode一樣的結果。對於null物件,identityHashCode返回零。