当前位置:   article > 正文

java shutdown_java.lang.Shutdown | 学步园

java.lang.shutdown

package java.lang;

import java.util.HashSet;

import java.util.Iterator;

/**

*

*

* 管理关闭序列

*

* comment by liqiang

*

* @author   Mark Reinhold

*

*/

class Shutdown {

//一个静态内部类,用来封装一个Thread对象,保证两个线程完全相等(通过比较

//内存地址相等,而不是通过equals方法的相等)

private static class WrappedHook {

private Thread hook;

WrappedHook(Thread t) {

hook = t;

}

public int hashCode() {

return System.identityHashCode(hook);

}

public boolean equals(Object o) {

//如果不是此类对象(不是此类对象内存地址肯定不等)直接返回false

if (!(o instanceof WrappedHook)) return false;

//比较内存地址是否相等

return (((WrappedHook)o).hook == hook);

}

}

//关闭状态

private static final int RUNNING = 0;

private static final int HOOKS = 1;

private static final int FINALIZERS = 2;

//单前的初始状态为RUNNING

private static int state = RUNNING;

//是否在退出时运行清除操作

private static boolean runFinalizersOnExit = false;

/* The set of registered, wrapped hooks, or null if there aren't any */

//关闭钩注册的地方,初始化为null表示不含有关闭钩

private static HashSet hooks = null;

//静态内部类,用来生成同步的锁

private static class Lock { };

//一个静态对象表示锁

private static Object lock = new Lock();

//是否在退出时运行清除操作的标志

static void setRunFinalizersOnExit(boolean run) {

synchronized (lock) {

runFinalizersOnExit = run;

}

}

/*

*

* 添加一个新的的关闭钩

*

*/

static void add(Thread hook) {

synchronized (lock) {

//如果当前ShutDown对象不是运行状态,抛出异常

if (state > RUNNING)

throw new IllegalStateException("Shutdown in progress");

//如果线程还Alive则抛出异常

if (hook.isAlive())

throw new IllegalArgumentException("Hook already running");

if (hooks == null) {

//hooks Set为空则创建新的hooks Set并将此线程对象封装成WrappedHook加入

hooks = new HashSet(11);

hooks.add(new WrappedHook(hook));

Terminator.setup();

} else {

//封装成WrappedHook对象

WrappedHook wh = new WrappedHook(hook);

//如果此封装对象已经存在则抛出异常

if (hooks.contains(wh))

throw new IllegalArgumentException("Hook previously registered");

//添加此封装对象

hooks.add(wh);

}

}

}

/*

*

* 删除注册过的关闭钩

*

*/

static boolean remove(Thread hook) {

synchronized (lock) {

//如果此对象不是运行状态抛出异常

if (state > RUNNING)

throw new IllegalStateException("Shutdown in progress");

if (hook == null) throw new NullPointerException();

//如果hooks为null表示没有注册过关闭钩则直接返回false

if (hooks == null) {

return false;

} else {

//从set中删除相应的WrappedHook

boolean rv = hooks.remove(new WrappedHook(hook));

//如果删除成功,且set为空,则将set清空

if (rv && hooks.isEmpty()) {

hooks = null;

//此方法现在不支持

Terminator.teardown();

}

return rv;

}

}

}

/*

*

* 运行所有注册的关闭钩

*

*/

private static void runHooks() {

if (hooks == null) return;

//启动注册的线程

for (Iterator i = hooks.iterator(); i.hasNext();) {

((WrappedHook)(i.next())).hook.start();

}

//等待每个线程死去

for (Iterator i = hooks.iterator(); i.hasNext();) {

try {

((WrappedHook)(i.next())).hook.join();

} catch (InterruptedException x) {

continue;

}

}

}

//停止

static native void halt(int status);

//运行清除操作

private static native void runAllFinalizers();

/*

* 此方法定义了关闭的序列

*/

private static void sequence() {

synchronized (lock) {

if (state != HOOKS) return;

}

//运行所有注册的hook

runHooks();

boolean rfoe;

synchronized (lock) {

//设置状态为FINALIZERS

state = FINALIZERS;

rfoe = runFinalizersOnExit;

}

//如果在虚拟机退出时进行清除操作则运行此操作

if (rfoe) runAllFinalizers();

}

/*

* 关闭虚拟机,由Runtime.exit调用

*/

static void exit(int status) {

boolean runMoreFinalizers = false;

synchronized (lock) {

//如果不是运行状态,则设置状态为退出时不做清除操作

if (status != 0) runFinalizersOnExit = false;

//根据不同状态做不同的处理

switch (state) {

case RUNNING: //设置为HOOKS状态

state = HOOKS;

break;

case HOOKS:  //不作操作

break;

case FINALIZERS: //FINALIZERS情况

if (status != 0) {

//停止

halt(status);

} else {

runMoreFinalizers = runFinalizersOnExit;

}

break;

}

}

if (runMoreFinalizers) {

//退出时需要做清除操作

runAllFinalizers();

//停止

halt(status);

}

synchronized (Shutdown.class) {

//顺序关闭

sequence();

//停止

halt(status);

}

}

static void shutdown() {

synchronized (lock) {

switch (state) {

case RUNNING: /* Initiate shutdown */

state = HOOKS;

break;

case HOOKS:  /* Stall and then return */

case FINALIZERS:

break;

}

}

synchronized (Shutdown.class) {

sequence();

}

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/265975
推荐阅读
相关标签
  

闽ICP备14008679号