栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JUC并发编程 -- 任务调度线程池 定时任务 / 延时执行(ScheduledThreadPoolExecutor 延时执行 / 定时执行)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JUC并发编程 -- 任务调度线程池 定时任务 / 延时执行(ScheduledThreadPoolExecutor 延时执行 / 定时执行)

1. 任务调度线程池
1.1 ScheduledThreadPoolExecutor 延时执行

示例代码(任务都延时1s执行):

package com.tian;

import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TestTimer {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
        // 添加两个任务,希望它们都在 1s 后执行
        System.out.println("当前时间:     " + new Date().toLocaleString());
        executor.schedule(() -> {
            System.out.println("任务1执行时间:" + new Date().toLocaleString());
            try {
                Thread.sleep(5000);
                System.out.println("任务1执行完毕" + new Date().toLocaleString());
            } catch (InterruptedException e) {
            }
        }, 1000, TimeUnit.MILLISECONDS);
        executor.schedule(() -> {
            System.out.println("任务2执行时间:" + new Date().toLocaleString());
            System.out.println("任务2执行完毕" + new Date().toLocaleString());
        }, 1000, TimeUnit.MILLISECONDS);
    }
}

运行结果:


1.2 ScheduledThreadPoolExecutor 定时执行
1.2.1 scheduleAtFixedRate
1.2.1.1 任务执行时间小于周期(周期不变)

在1s后执行任务 之后每2s执行一次任务

package com.tian;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j(topic = "TestTimer")
public class TestTimer {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        log.debug("start...");
        // 在1s后执行任务 之后每2s执行一次任务
        pool.scheduleAtFixedRate(() -> {
            log.debug("running...");
        }, 1, 2, TimeUnit.SECONDS);
    }
}

运行结果:


1.2.2.2 任务执行时间大于周期(此时周期为任务的执行时间)

示例代码:

package com.tian;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j(topic = "TestTimer")
public class TestTimer {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        log.debug("start...");
        // 在1s后执行任务 之后每2s执行一次任务
        pool.scheduleAtFixedRate(() -> {
            log.debug("running...");
            try {
                // 任务休眠时间(3s)大于执行周期(2s)
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, 1, 2, TimeUnit.SECONDS);
    }
}

运行结果:


1.2.2.3 当任务执行遇到异常时(程序会一直停留在异常这里)

示例代码:

package com.tian;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j(topic = "TestTimer")
public class TestTimer {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        log.debug("start...");
        // 在1s后执行任务 之后每2s执行一次任务
        pool.scheduleAtFixedRate(() -> {
            log.debug("running...");
            int number = 1 / 0;
        }, 1, 2, TimeUnit.SECONDS);
    }
}

执行结果:


1.3.2 scheduleWithFixedDelay

记住: scheduleWithFixedDelay中任务真正的执行周期为: 任务的执行时间 + 执行周期

package com.tian;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j(topic = "TestTimer")
public class TestTimer {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        log.debug("start...");
        pool.scheduleWithFixedDelay(() -> {
            log.debug("running...");
            try {
                // 任务睡眠5s
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 任务在1s后执行 执行周期为2s
        }, 1, 2, TimeUnit.SECONDS);
    }
}

其余的和scheduleAtFixedRate类似,多说无益



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/286926.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号