• [Druid-1.2.11源码系列]-9-Druid销毁线程


    9-Druid销毁线程

    9.1 简介

    在前面的博文中我们介绍了创建Druid连接的线程的逻辑《6-DruidDataSource物理连接创建线程createAndStartCreatorThread》 ,这里来看下销毁连接的逻辑。
    这个代码调用逻辑是来源于前面说的DruidDataSource的连接对象获取时的初始化方法init
    调用代码如下:

     createAndStartDestroyThread();
    
    • 1

    9.2 创建销毁线程的具体逻辑

    直接来看DruidDataSource类型的createAndStartDestroyThread代码如下所示:

      protected void createAndStartDestroyThread() {
           //创建销毁任务
            destroyTask = new DestroyTask();
    
    		//这里我们没有配置销毁定时器
            if (destroyScheduler != null) {
                long period = timeBetweenEvictionRunsMillis;
                if (period <= 0) {
                    period = 1000;
                }
                destroySchedulerFuture = destroyScheduler.scheduleAtFixedRate(destroyTask, period, period,
                        TimeUnit.MILLISECONDS);
                initedLatch.countDown();
                return;
            }
    
            String threadName = "Druid-ConnectionPool-Destroy-" + System.identityHashCode(this);
            //创建与启动销毁线程
            destroyConnectionThread = new DestroyConnectionThread(threadName);
            destroyConnectionThread.start();
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    9.3 销毁线程代码

    这里直接来看代码了

    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;
                    }
                }
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    9.4 销毁任务代码

    看下销毁任务的代码:

     public class DestroyTask implements Runnable {
            public DestroyTask() {
            }
    
            @Override
            public void run() {
                shrink(true, keepAlive);
    
                if (isRemoveAbandoned()) {
                    removeAbandoned();
                }
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    shrink

    移除空闲连接removeAbandoned

  • 相关阅读:
    【牛客】VL74 异步复位同步释放
    Coreldraw2020最新64位电脑完整版本下载教程
    云呐|如何利用系统管理固定资产?如何进行固定资产管理?
    基于springboot 的 Ajax-fetchget post && Axios-get post
    Flink SQL Hudi 实战
    android 12 framework开发第53节-Activity的reLaunch及onConfigurationChanged android源码分析
    deque(双端数组)——STL
    文本生成解码策略
    web3.0学习入门1:什么是web3.0
    【王道】计算机组成原理第五章中央处理器(五)
  • 原文地址:https://blog.csdn.net/songjunyan/article/details/125531032