代码执行流程
 DruidDataSource.init(){
DruidDataSource.createAndLogThread()
DruidDataSource.createAndStartCreatorThread()
DruidDataSource.createAndStartDestroyThread()
DruidDataSource.DestroyConnectionThread.run() {
com.alibaba.druid.pool.DruidDataSource.DestroyTask.run() {
com.alibaba.druid.pool.DruidDataSource.shrink(boolean checkTime, boolean keepAlive){}
}
}}
       复制代码
 以上为伪代码
下面为内部类 DestroyConnectionThread
     public class DestroyConnectionThread extends Thread {
        public DestroyConnectionThread(String name){            super(name);            this.setDaemon(true);        }
        public void run() {            initedLatch.countDown();
            for (;;) {                // 从前面开始删除                try {                    if (closed || closing) {                        break;                    }
                    if (timeBetweenEvictionRunsMillis > 0) {                        Thread.sleep(timeBetweenEvictionRunsMillis);                    } else {                        Thread.sleep(1000); //                    }
                    if (Thread.interrupted()) {                        break;                    }
                    destroyTask.run();                } catch (InterruptedException e) {                    break;                }            }        }
    }
       复制代码
 Druid-ConnectionPool-Destroy 守护线进行无限循环,每次循环中进行 sleep。 Druid-ConnectionPool-Destroy 守护线程每次循环都会调用  DestroyTask.run(),DestroyTask.run()调用 com.alibaba.druid.pool.DruidDataSource.shrink(boolean checkTime, boolean keepAlive) 执行 druid 保活逻辑。
评论