Java并发之ScheduledExecutorService(schedule、scheduleAtFixedRate、scheduleWithFixedDelay)
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)的更多相关文章
- ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别
ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别 ScheduledThreadPoolExecut ...
- Java并发之BlockingQueue的使用
Java并发之BlockingQueue的使用 一.简介 前段时间看到有些朋友在网上发了一道面试题,题目的大意就是:有两个线程A,B, A线程每200ms就生成一个[0,100]之间的随机数, B线 ...
- JAVA线程池ScheduledExecutorService周期性地执行任务 与单个Thread周期性执行任务的异常处理
本文记录: 1,使用ScheduledExecutorService的 scheduleAtFixedRate 方法执行周期性任务的过程,讨论了在任务周期执行过程中出现了异常,会导致周期任务失败. 2 ...
- Java中timer的schedule()和schedualAtFixedRate()函数的区别
本文主要讨论java.util.Timer的schedule(timerTask,delay,period)和scheduleAtFixedRate(timerTask,delay,period)的区 ...
- java并发之固定对象与实例
java并发之固定对象与实例 Immutable Objects An object is considered immutable if its state cannot change after ...
- 深入理解Java并发之synchronized实现原理
深入理解Java类型信息(Class对象)与反射机制 深入理解Java枚举类型(enum) 深入理解Java注解类型(@Annotation) 深入理解Java类加载器(ClassLoader) 深入 ...
- Java并发之Semaphore的使用
Java并发之Semaphore的使用 一.简介 今天突然发现,看着自己喜欢的球队发挥如此的棒,然后写着博客,这种感觉很爽.现在是半场时间,就趁着这个时间的空隙,说说Java并发包中另外一个重量级的类 ...
- Java并发之CyclicBarria的使用(二)
Java并发之CyclicBarria的使用(二) 一.简介 之前借助于其他大神写过一篇关于CyclicBarria用法的博文,但是内心总是感觉丝丝的愧疚,因为笔者喜欢原创,而不喜欢去转载一些其他的文 ...
- Java并发之CyclicBarria的使用
Java并发之CyclicBarria的使用 一.简介 笔者在写CountDownLatch这个类的时候,看到了博客园上的<浅析Java中CountDownLatch用法>这篇博文,为博主 ...
随机推荐
- jquery简单原则器(匹配除了指定选择器之外的元素 selector 表示选择器)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 一个十年java程序员的心得
展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告 走过的路,回忆起来是那么曲折,把自己的一些心得体会分享给程序员兄弟姐妹们,虽然时代在变化,但是很可能你也会走我已经做过的1 ...
- DokuWiki整合Zentao的用户授权及分组体系
老外们把精力都放在了怎样做通用性上面了. Doku后台有切换授权方式的选项,改成mysql. 注:如下修改mysql.conf.php后,要把分组和权限设置结合起来,还需要配置dokuwiki的分组, ...
- 第 26 章 CSS3 动画效果
学习要点: 1.动画简介 2.属性详解 3.简写和版本 主讲教师:李炎恢 本章主要探讨 HTML5 中 CSS3 的动画效果,可以通过类似 Flash 那样的关键帧模式控制运行. 一.动画简介 CSS ...
- 2015暑假多校联合---Problem Killer(暴力)
原题链接 Problem Description You are a "Problem Killer", you want to solve many problems. Now ...
- No.020:Valid Parentheses
问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- 解决Wireshark没有网卡问题
wireshark在mac上使用没有网卡问题直接在终端输入命令:sudo chmod 644 /dev/bpf*
- [deviceone开发]-优惠券商户管理端App开源
一.简介 这是一个优惠券的商主端,也就是配置发送优惠券的App 页面和交互还是像纳豆那样非常漂亮流畅,大家可以参考一下 二.效果图 三.源码分享 https://github.com/do-proje ...
- 之一:CABasicAnimation - 基本动画
嗷呜嗷呜嗷呜 // 将视图作为属性方便后面执行多个不同动画 _myView = [[UIView alloc] init]; _myView.layer.position = CGPointMake( ...
- [Android]竖直滑动选择器WheelView的实现
以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3819304.html 公司项目中有这么一个需求,所以需要自己实现下.效 ...