线程的定时任务类ScheduledExecutorService

ScheduledExecutorService是Java线程中的定时任务类,常用于周期性或延迟执行任务。Dubbo源码中的ScheduledExecutorService用于在与注册中心断开连接后自动尝试重连。守护线程(Daemon Thread)是后台运行的特殊线程,当所有用户线程结束时,守护线程也随之结束。
摘要由CSDN通过智能技术生成

线程的定时任务类ScheduledExecutorService

前言

最近再看dubbo源码的时候(DubboRegistry),有这么一段

// 定时任务执行器
private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1, new NamedThreadFactory("DubboRegistryReconnectTimer", true));

// 重连定时器,定时检查连接是否可用,不可用时,无限次重连
private final ScheduledFuture<?> reconnectFuture;

/..../

public DubboRegistry(Invoker<RegistryService> registryInvoker, RegistryService registryService) {
        super(registryInvoker.getUrl());
        this.registryInvoker = registryInvoker;
        this.registryService = registryService;
        // 启动重连定时器
        int reconnectPeriod = registryInvoker.getUrl().getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, RECONNECT_PERIOD_DEFAULT);
        reconnectFuture = scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
            public v
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值