System类详解-2 exit gc runFinalization identityHashCode

来源:互联网 发布:网络支付软件有哪些 编辑:程序博客网 时间:2024/06/03 20:15

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返回零。






原创粉丝点击