Timer和ScheduledThreadPoolExecutor小例子
来源:互联网 发布:淘宝卖家店铺名怎么改 编辑:程序博客网 时间:2024/04/27 05:00
<pre name="code" class="java"> //1、固定速率执行//A - 如果执行时间 > 周期执行间隔事件,则马上执行//B - 如果执行时间 < 周期执行间隔时间,则延迟到到达间隔事件执行Timer timer = new Timer();timer.scheduleAtFixedRate(new TimerTask() {int count;SimpleDateFormat sf = new SimpleDateFormat("hh:mm:ss:SSS");@Overridepublic void run() {String nowTime;count++;try {nowTime = sf.format(new Date(System.currentTimeMillis()));System.out.println(count + "/" + nowTime);Thread.currentThread().sleep(500);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}, 500, 1000);//2、非速率执行//A - 如果执行时间 > 周期执行间隔事件,则马上执行//B - 如果执行时间 < 周期执行间隔时间,则延迟到到达间隔事件执行Timer timerTwo = new Timer();timerTwo.schedule(new TimerTask() {int count;SimpleDateFormat sf = new SimpleDateFormat("hh:mm:ss:SSS");@Overridepublic void run() {String nowTime;count++;try {nowTime = sf.format(new Date(System.currentTimeMillis()));System.out.println(count + "/" + nowTime);Thread.currentThread().sleep(500);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}, 0, 1000);//1、scheduleAtFixedRate周期执行//A - 如果执行时间 > 周期执行间隔事件,则马上执行//B - 如果执行时间 < 周期执行间隔时间,则延迟到到达间隔事件执行ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(3);pool.scheduleAtFixedRate(new Runnable() {int count;SimpleDateFormat sf = new SimpleDateFormat("hh:mm:ss:SSS");@Overridepublic void run() {String nowTime;count++;try {nowTime = sf.format(new Date(System.currentTimeMillis()));System.out.println(count + "/" + nowTime);Thread.currentThread().sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}, 0, 1000, TimeUnit.MILLISECONDS);//1、scheduleWithFixedDelay周期执行//A - 如果执行时间 > 周期执行间隔事件,任务结束后还需要等待到间隔事件之后才执行//B - 如果执行时间 < 周期执行间隔时间,任务结束后还需要等待到间隔事件之后才执行ScheduledThreadPoolExecutor poolTwo = new ScheduledThreadPoolExecutor(3);poolTwo.scheduleWithFixedDelay(new Runnable() {int count;SimpleDateFormat sf = new SimpleDateFormat("hh:mm:ss:SSS");@Overridepublic void run() {String nowTime;count++;try {nowTime = sf.format(new Date(System.currentTimeMillis()));System.out.println(count + "/" + nowTime);Thread.currentThread().sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}, 0, 2000, TimeUnit.MILLISECONDS);
0 0
- Timer和ScheduledThreadPoolExecutor小例子
- Timer和ScheduledThreadPoolExecutor比较
- Timer和ScheduledThreadPoolExecutor
- Timer和ScheduledThreadPoolExecutor的区别
- Timer和ScheduledThreadPoolExecutor的区别
- Timer和ScheduledThreadPoolExecutor的区别
- Timer和ScheduledThreadPoolExecutor的区别
- Timer和ScheduledThreadPoolExecutor的区别
- <转载> Timer和ScheduledThreadPoolExecutor的区别
- BlockingQueue、thread.join()、Semaphore、Timer 和ScheduledThreadPoolExecutor
- java Timer小例子
- ScheduledThreadPoolExecutor 取代timer
- Timer与ScheduledThreadPoolExecutor
- java timer and ScheduledThreadPoolExecutor
- Timer与ScheduledThreadPoolExecutor
- 使用ScheduledThreadPoolExecutor 替代 Timer
- 使用ScheduledThreadPoolExecutor代替Timer&TimerTask
- 使用ScheduledThreadPoolExecutor代替Timer&TimerTask
- 非静态成员函数的非法调用错误
- android4.1多媒体本地播放流程video playback (四)
- android 服务与多线程
- 京东11.11:交易系统的关键技术
- 第十二周项目四(2)童氏银行完整版
- Timer和ScheduledThreadPoolExecutor小例子
- ISO language name map LCID
- windows平台下搭建lighttpd+php+sqlite
- 关于富头像上传的使用方法
- Android中的“再按一次返回键退出程序”实现
- Android 菜单, option menu 和 context menu (右键菜单)
- activex控件开发时如何用ie8进行调试
- 自定义弧形渐变进度条功能
- hadoop1.1.2配置文件