例子1(scheduleAtFixedRate):延迟2秒后,每隔3秒执行1次

ScheduledExecutorService es = Executors.newScheduledThreadPool(5);
log.info("开始时间");
Runnable syncRunnable = new Runnable() {
@Override
public void run() {
log.info(Thread.currentThread().getName());
}
};
es.scheduleAtFixedRate(syncRunnable, 2000, 3000, TimeUnit.MILLISECONDS);

运行结果:

            10:49:22.495 开始时间
10:49:24.500 pool-1-thread-1
10:49:27.499 pool-1-thread-1
10:49:30.500 pool-1-thread-2
10:49:33.500 pool-1-thread-1
10:49:36.501 pool-1-thread-3
10:49:39.500 pool-1-thread-2
10:49:42.500 pool-1-thread-2
10:49:45.500 pool-1-thread-1
10:49:48.501 pool-1-thread-1
10:49:51.501 pool-1-thread-1
10:49:54.501 pool-1-thread-4
10:49:57.501 pool-1-thread-2
10:50:00.502 pool-1-thread-2
10:50:03.502 pool-1-thread-3
10:50:06.502 pool-1-thread-3
10:50:09.502 pool-1-thread-3
10:50:12.503 pool-1-thread-5
10:50:15.503 pool-1-thread-5
10:50:18.503 pool-1-thread-5
10:50:21.503 pool-1-thread-5
10:50:24.503 pool-1-thread-3
10:50:27.503 pool-1-thread-2
10:50:30.503 pool-1-thread-1
10:50:33.504 pool-1-thread-4
10:50:36.504 pool-1-thread-5
10:50:39.504 pool-1-thread-5
10:50:42.504 pool-1-thread-2

例子2(scheduleWithFixedDelay):延迟5秒后,每个任务执行完后延迟3秒在执行1次

ScheduledExecutorService es = Executors.newScheduledThreadPool(5);
log.info("开始时间");
Runnable syncRunnable = new Runnable() {
@Override
public void run() {
log.info(Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
es.scheduleWithFixedDelay(syncRunnable, 5000, 3000, TimeUnit.MILLISECONDS);

运行结果:

            11:10:27.773 开始时间
11:10:32.778 pool-1-thread-1
11:10:36.779 pool-1-thread-1
11:10:40.780 pool-1-thread-2
11:10:44.781 pool-1-thread-1
11:10:48.783 pool-1-thread-3
11:10:52.785 pool-1-thread-3
11:10:56.785 pool-1-thread-4
11:11:00.787 pool-1-thread-4
11:11:04.788 pool-1-thread-4
11:11:08.789 pool-1-thread-4
11:11:12.790 pool-1-thread-3
11:11:16.792 pool-1-thread-1

本来是每隔3秒执行的,但是,由于某个任务处理时间过长,导致延后。本例是延后1秒,即4秒。

总结:scheduleAtFixedRate与scheduleWithFixedDelay区别

scheduleAtFixedRate:不管任务是否执行完了,在3秒内必须执行
scheduleWithFixedDelay:等任务执行完了,在等3秒后执行
因此,scheduleWithFixedDelay 非常有用。

线程池(5)Executors.newScheduledThreadPool的更多相关文章

  1. 线程池之 Executors

    线程池之 Executors + 面试题 线程池的创建分为两种方式:ThreadPoolExecutor 和 Executors,上一节学习了 ThreadPoolExecutor 的使用方式,本节重 ...

  2. Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor

    介绍new Thread的弊端及Java四种线程池的使用,对Android同样适用.本文是基础篇,后面会分享下线程池一些高级功能. 1.new Thread的弊端执行一个异步任务你还只是如下new T ...

  3. Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor

    1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? Java new Thread(new Runnable() { @Override public void ru ...

  4. 线程池工厂Executors编程的艺术

    Executors是一个线程池的工厂类,提供各种有用的线程池的创建,使用得当,将会使我们并发编程变得简单!今天就来聊聊这个工厂类的艺术吧! Executors只是Executor框架的主要成员组件之一 ...

  5. Java线程池ThreadPoolExecutor&&Executors

    一.先看看传统的开启线程. new Thread(new Runnable() { @Override public void run() { } }).start(); 缺点: 1.每次new Th ...

  6. Java并发包线程池之Executors、ExecutorCompletionService工具类

    前言 前面介绍了Java并发包提供的三种线程池,它们用处各不相同,接下来介绍一些工具类,对这三种线程池的使用. Executors Executors是JDK1.5就开始存在是一个线程池工具类,它定义 ...

  7. 线程池(2)-Executors提供4个线程池

    1.为什么不使用Executors提供4个线程池创建线程池 阿里巴巴开放手册这样写: . [强制]线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式, ...

  8. 线程池工厂方法newScheduledThreadPool(),计划任务

    package com.thread.test.ThreadPool; import java.util.concurrent.Executors; import java.util.concurre ...

  9. Android线程池使用终结版

    有一段时间没写博文了,今天抽空总结一下,也希望能通过自己写的这些文章,加深理解的同时能帮 助在技术方面有疑点的朋友搞清楚个所以然来,由于经常会在网上或群里看到有朋友会问线程方面的 东西,就像我一个朋友 ...

  10. Java-五种线程池,四种拒绝策略,三种阻塞队列(转)

    Java-五种线程池,四种拒绝策略,三种阻塞队列 三种阻塞队列:    BlockingQueue<Runnable> workQueue = null;    workQueue = n ...

随机推荐

  1. 常用JS组件整理

    1.漂亮的弹出层----artDialog http://aui.github.io/artDialog/ 2.弹出层 ------layer http://sentsin.com/jquery/la ...

  2. 分享知识-快乐自己:SpringBoot 使用注解API的方式定义启动端口号

    在Spring Boot2.0以上配置嵌入式Servlet容器时EmbeddedServletContainerCustomizer类不存在,经网络查询发现被WebServerFactoryCusto ...

  3. redis实现分布式锁——核心 setx+pipe watch监控key变化-事务

    如何设计一把分布式锁 我们用 redis 来实现这把分布式的锁,redis 速度快.支持事务.可持久化的特点非常适合创建分布式锁. 分布式环境中如何消除网络延迟对锁获取的影响 锁,简单来说就是存于 r ...

  4. Gym:101630J - Journey from Petersburg to Moscow(最短路)

    题意:求1到N的最短路,最短路的定义为路径上最大的K条边. 思路:对于每种边权,假设为X,它是第K大,那么小于X的变为0,大于K的,边权-X.然后求最短路,用dis[N]+K*X更新答案. 而小于K的 ...

  5. 1072 Gas Station (30)(30 分)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  6. 1034 Head of a Gang (30)(30 分)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  7. HDU4027(线段树单点更新区间)

    Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K ...

  8. ural 1500 Pass Licenses (状态压缩+dfs)

    1500. Pass Licenses Time limit: 2.5 secondMemory limit: 64 MB A New Russian Kolyan believes that to ...

  9. ios之CoreAnimation

    CoreAnimation的好处: 1.高性能,简单的编程模块 2.像View一样,使用层级结构来构建负责的界面 3.轻量级数据结构,能使上百个动画同时执行 4.抽象的动画接口,允许动画在一个独立的线 ...

  10. solr-建立单机版的服务器

    回到之前打开的页面,刷新,wenda就出来了: 这个wenda是单机版的.