【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
...

你可能感兴趣的:(#,JAVA并发编程系列)