package com.thread.test.thread;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock; /**
* schedule(Runnable command, long delay, TimeUnit unit)
* @ command: 需要执行的任务
* @ delay:任务执行需要延迟的时间
* @ unit:时间单位
*
* 一次性执行任务,执行完成结束
*
* ScheduledExecutorService:
* scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
* @ Runnable command: 需要执行的任务
* @ long initialDelay:第一次执行延迟的时间
* @ long period:间隔周期
* @ TimeUnit unit
*
* 包含首次延迟的周期性执行任务,第一次执行:delay+period,第二次:delay+2*period,以此类推...
* 停止:异常停止执行,主动调用停止方法
* 如果某一个周期执行时间超过设定的period,则后续顺延
*
* scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
* @ command: 需要执行的任务
* @ initialDelay:第一次执行延迟的时间
* @ delay:周期之间的延迟,间隔
* @ unit:前两个参数的单位
*
* 周期性执行任务:第一次执行:initialDelay+delay,第二次:initialDelay+2*delay,以此类推...
* 停止:异常停止执行,主动调用停止方法
* 不顺延
*
* Created by windwant on 2016/5/26.
*/
public class MyExecutor {
public static void main(String[] args) {
testTimer();
} public static void testTimer(){
new Timer().schedule(new MyTimerTask(), 2000, 5000);
} public static void testExecutors(){
MyERunnable mer = new MyERunnable(5);
// ExecutorService es = Executors.newCachedThreadPool();
// ExecutorService es = Executors.newFixedThreadPool(2);
ScheduledExecutorService es = Executors.newScheduledThreadPool(2);
es.schedule(mer, 10000, TimeUnit.SECONDS.MILLISECONDS);
es.scheduleAtFixedRate(mer, 2, 10, TimeUnit.SECONDS);
es.scheduleWithFixedDelay(mer, 1, 5, TimeUnit.SECONDS);
es.shutdown();
}
} class MyERunnable implements Runnable{
private int num = 0;
MyERunnable(int num){
this.num = num;
}
public void run() {
ReentrantLock lock = new ReentrantLock();
try{
lock.lock();
for (int i = 0; i < num; i++) {
System.out.println("current thread: " + Thread.currentThread().getName() + " num--" + i);
Thread.sleep(1000);
}
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
} class MyTimerTask extends TimerTask{ @Override
public void run() {
System.out.println("timer task");
}
}

项目地址:https://github.com/windwant/windwant-demo/tree/master/thread-demo

Java并发之ScheduledExecutorService(schedule、scheduleAtFixedRate、scheduleWithFixedDelay)的更多相关文章

  1. ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别

    ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别 ScheduledThreadPoolExecut ...

  2. Java并发之BlockingQueue的使用

    Java并发之BlockingQueue的使用 一.简介 前段时间看到有些朋友在网上发了一道面试题,题目的大意就是:有两个线程A,B,  A线程每200ms就生成一个[0,100]之间的随机数, B线 ...

  3. JAVA线程池ScheduledExecutorService周期性地执行任务 与单个Thread周期性执行任务的异常处理

    本文记录: 1,使用ScheduledExecutorService的 scheduleAtFixedRate 方法执行周期性任务的过程,讨论了在任务周期执行过程中出现了异常,会导致周期任务失败. 2 ...

  4. Java中timer的schedule()和schedualAtFixedRate()函数的区别

    本文主要讨论java.util.Timer的schedule(timerTask,delay,period)和scheduleAtFixedRate(timerTask,delay,period)的区 ...

  5. java并发之固定对象与实例

    java并发之固定对象与实例 Immutable Objects An object is considered immutable if its state cannot change after ...

  6. 深入理解Java并发之synchronized实现原理

    深入理解Java类型信息(Class对象)与反射机制 深入理解Java枚举类型(enum) 深入理解Java注解类型(@Annotation) 深入理解Java类加载器(ClassLoader) 深入 ...

  7. Java并发之Semaphore的使用

    Java并发之Semaphore的使用 一.简介 今天突然发现,看着自己喜欢的球队发挥如此的棒,然后写着博客,这种感觉很爽.现在是半场时间,就趁着这个时间的空隙,说说Java并发包中另外一个重量级的类 ...

  8. Java并发之CyclicBarria的使用(二)

    Java并发之CyclicBarria的使用(二) 一.简介 之前借助于其他大神写过一篇关于CyclicBarria用法的博文,但是内心总是感觉丝丝的愧疚,因为笔者喜欢原创,而不喜欢去转载一些其他的文 ...

  9. Java并发之CyclicBarria的使用

    Java并发之CyclicBarria的使用 一.简介 笔者在写CountDownLatch这个类的时候,看到了博客园上的<浅析Java中CountDownLatch用法>这篇博文,为博主 ...

随机推荐

  1. 部署Eclipse中的Web项目到Tomcat服务器运行

    用Eclipse开发Web项目时,可以通过Tomcat服务器运行Web项目,此时Web项目被部署在[WorkSpace]\.metadata\.plugins\org.eclipse.wst.serv ...

  2. contentResolver

    今天练习一个联系人的增,写,改,查的几项操作,其中一个重要的知识点  ContentResolver 还有一篇不错的参考文献 主要参见下面这个链接  http://cthhqu.blog.51cto. ...

  3. Android填坑系列:Android JSONObject 中对key-value为null的特殊处理

    在与服务端通过JSON格式进行交互过程中,不同版本的JSON库在对于key-value为null情况上的处理不同. Android自带的org.json对key-value都要求不能为null,对于必 ...

  4. Javascript单元测试Unit Testing之QUnit

    body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; }           QUnit是一个基于JQuery的单元测试Uni ...

  5. 项目中应用eventbus解决的问题

    在项目开发过程中,往往有些功能表面看起来简单,但实际开发的结果非常复杂,仔细分析下原因发现很多都是因为附加了许多的额外功能. 真的简单吗? 比如我们对一个电商平台的商品数据做修改的功能来讲,其实非常简 ...

  6. Captain Icon – 350+ 有趣的矢量图标免费下载

    Captain Icon 是一套一个惊人的免费图标集,包含350+有趣的矢量图标,可以缩放到任意大小而不会降低质量.图标的类别很丰富,有设计,体育,社会,天气等很多类别.提供 EPS.PSD.PNG. ...

  7. array's filter

    var arr = [ { id: 15 }, { id: -1 }, { id: 0 }, { id: 3 }, { id: 12.2 }, { }, { id: null }, { id: NaN ...

  8. go语言常用函数:make

    创建数组切片 Go语言提供的内置函数make()可以用于灵活地创建数组切片.创建一个初始元素个数为5的数组切片,元素初始值为0: mySlice1 := make([]int, 5) 创建一个初始元素 ...

  9. [deviceone开发]-基础文件管理器

    一.简介 主要实现本地文件管理功能,主要功能为复制.粘贴.剪切目录或者文件. 二.效果 三.相关下载 https://github.com/do-project/code4do/tree/master ...

  10. SharePoint 2013 中如何使用Silverlight

    1.打开VS,创建一个Silverlight程序,如下图: 2.配置选择默认的,当然也可以不勾选Host Application,如下图: 3.添加Silverlight控件,2个label和1个bu ...