【JUC】Executors 之 newSingleThreadScheduledExecutor 构建定时任务/延时任务

Executors 之 newSingleThreadScheduledExecutor 构建定时任务/延时任务

schedule

延时任务,只执行一次
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);

  • Runnable command (the task to execute)
  • long delay 延迟执行时间(the time from now to delay execution)
  • TimeUnit unit delay参数的时间单位(the time unit of the delay parameter)

scheduleAtFixedRate

延时任务,并循环执行
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);

  • Runnable command(the task to execute)
  • long initialDelay 执行第一次的延迟时间(the time to delay first execution)
  • long period 连续执行的间隔时间(the period between successive executions)
  • TimeUnit unit 前两个参数的时间单位(the time unit of the initialDelay and period parameters)

scheduleWithFixedDelay

延时任务,并循环执行
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);

  • Runnable command(the task to execute)
  • long initialDelay 执行第一次的延迟时间(the time to delay first execution)
  • long delay 上一个执行器的终止与下一个执行器的开始之间的延迟(the delay between the termination of one execution and the commencement of the next)
  • TimeUnit unit delay参数的时间单位(the time unit of the delay parameter)

talk is cheap, show me the code.

System.out.format("[%s] - [%s] - main\n",
	DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now()),
	Thread.currentThread().getName());

// 创建一个单线程的任务调度池
ScheduledExecutorService singleThreadScheduledPool = Executors.newSingleThreadScheduledExecutor();

// 延时5秒后执行,只执行一次
singleThreadScheduledPool.schedule(() -> {
	System.out.format("[%s] - [%s] - schedule\n",
		DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now()),
		Thread.currentThread().getName());
}, 5, TimeUnit.SECONDS);

// 延时一秒后执行第一次,再循环执行(每隔5秒执行一次)
singleThreadScheduledPool.scheduleAtFixedRate(() -> {
	System.out.format("[%s] - [%s] - scheduleAtFixedRate\n",
		DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now()),
		Thread.currentThread().getName());
}, 1, 5, TimeUnit.SECONDS);

// 延时一秒后执行第一次,再循环执行(上一次执行结束延时5秒后再执行下次任务)
singleThreadScheduledPool.scheduleWithFixedDelay(() -> {
	System.out.format("[%s] - [%s] - scheduleWithFixedDelay\n",
		DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now()),
		Thread.currentThread().getName());
}, 1, 5, TimeUnit.SECONDS);

控制台输出

[2020/05/28 10:53:09] - [main] - main
[2020/05/28 10:53:10] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:10] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:14] - [pool-1-thread-1] - schedule
[2020/05/28 10:53:15] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:15] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:20] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:20] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:25] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:25] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:30] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:30] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:35] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:35] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:40] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:40] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:45] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:45] - [pool-1-thread-1] - scheduleWithFixedDelay
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值