zoukankan      html  css  js  c++  java
  • 通过反射的方式注入自己的ShutdownHook并清除其他HOOK

    String className = "java.lang.ApplicationShutdownHooks";
    Class<?> clazz = Class.forName(className);
    Field field = clazz.getDeclaredField("hooks");
    field.setAccessible(true);
    
    Thread shutdownThread = new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO
        }
    });
    shutdownThread.setName("My-WebShutdownThread");
    IdentityHashMap<Thread, Thread> excludeIdentityHashMap = new ExcludeIdentityHashMap<>();
    excludeIdentityHashMap.put(shutdownThread, shutdownThread);
    
    synchronized (clazz) {
        IdentityHashMap<Thread, Thread> map = (IdentityHashMap<Thread, Thread>) field.get(clazz);
        for (Thread thread : map.keySet()) {
            Log.info("found shutdownHook: " + thread.getName());
            excludeIdentityHashMap.put(thread, thread);
        }
    
        field.set(clazz, excludeIdentityHashMap);
    }
  • 相关阅读:
    Excel Add-in
    并发控制MsSql
    Kaggle实战分类问题2
    NuGet
    Pomelo分布式游戏服务器框架
    Ambari
    oracle 多行转多列查询
    Oauth2.0 用Spring-security-oauth2
    bug排查小结
    Linux之lsof命令
  • 原文地址:https://www.cnblogs.com/hujunzheng/p/8573216.html
Copyright © 2011-2022 走看看