定时任务ScheduledThreadPoolExecutor的使用详解
来源:互联网 发布:淘宝章鱼哥代购靠谱吗 编辑:程序博客网 时间:2024/06/05 15:17
定时任务ScheduledThreadPoolExecutor的使用详解
前短时间需要用到一个定时器处理蓝牙设备接收的数据,并且需要处理的频率是很快的,这就需要一个稳定的定时器来保证数据的长久进行。ScheduledThreadPoolExecutor这个类就是个很好的选择。
正常情况下,定时器我们都是用Timer和TimerTask这两个类就能完成定时任务,并且设置延长时间和循环时间间隔。
ScheduledThreadPoolExecutor也能完成Timer一样的定时任务,并且时间间隔更加准确。
误差说明:
我在后台程序看看一下Timer执行程序是有可能延迟1、2毫秒,如果是1秒执行一次的任务,1分钟有可能延迟60毫秒,一小时延迟3600毫秒,相当于3秒,实际用户看不出什么区别。
但是,如果我的程序需要每40毫秒就执行一次任务,如果还是有1、2毫秒的误差,1秒钟就有25毫秒的误差,大概40秒就有1秒的误差,十几分钟就有十几秒的误差,这对UI显示来说是延迟非常严重的了。
而我用ScheduledThreadPoolExecutor来做40毫秒的间隔任务,一般十几分钟才有1秒多的误差,这个还是能接受的。
这也是我用ScheduledThreadPoolExecutor这个类的原因。
ScheduledThreadPoolExecutor的介绍
ScheduledThreadPoolExecutor,它可另行安排在给定的延迟后运行命令,或者定期执行命令。需要多个辅助线程时,或者要求 ThreadPoolExecutor 具有额外的灵活性或功能时,此类要优于Timer。
使用Timer和TimerTask存在一些缺陷:
1.Timer只创建了一个线程。当你的任务执行的时间超过设置的延时时间将会产生一些问题。
2.Timer创建的线程没有处理异常,因此一旦抛出非受检异常,该线程会立即终止。
JDK 5.0以后推荐使用ScheduledThreadPoolExecutor。该类属于Executor Framework,它除了能处理异常外,还可以创建多个线程解决上面的问题
Timer和TimerTask的使用
这个大家应该都是会使用的了,但是还是給给大家看看代码:
Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { log.e("time:"); } }, 2000, 40);//2000表示第一次执行任务延迟时间,40表示以后每隔多长时间执行一次run里面的任务
ScheduledThreadPoolExecutor的使用
这里先学会简单使用再深入探讨。
ScheduledThreadPoolExecutor scheduled = new ScheduledThreadPoolExecutor(2); scheduled.scheduleAtFixedRate(new Runnable() { @Override public void run() { loge("time:"); } }, 0, 40, TimeUnit.MILLISECONDS);//0表示首次执行任务的延迟时间,40表示每次执行任务的间隔时间,TimeUnit.MILLISECONDS执行的时间间隔数值单位
间隔单位毫秒:TimeUnit.MILLISECONDS
间隔单位秒:TimeUnit.SECONDS
间隔单位分钟:TimeUnit.MINUTES
间隔单位小时:TimeUnit.HOURS
间隔单位天:TimeUnit.DAYS
定时器的一个实例
使用Timer和TimerTask、ScheduledThreadPoolExecutor显示秒的定时器
效果:
这里看起来是每秒刷新一次UI,但是我为了看40毫秒的延迟效果,是每40 毫秒index加一,添加25次后,刷新一次UI,表面上看起来是和每秒刷新的效果。
代码:
package com.example.wenzhi.scheduledthreadpoolexecutordemo;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.TextView;import java.util.Timer;import java.util.TimerTask;import java.util.concurrent.ScheduledThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class MainActivity extends AppCompatActivity { TextView tv_time; int time; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_time = (TextView) findViewById(R.id.tv_time); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (time % 25 == 0) { tv_time.setText("" + time / 25); } } }; /** * 普通定时器 */ Timer timer; public void timer(View view) { stopTimer1(); timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { loge("time:" + time++);//add handler.sendEmptyMessage(11); } }, 2000, 40); } public void stop(View view) { stopTimer1(); } /** * 停止定时器 */ private void stopTimer1() { if (timer != null) { timer.cancel(); timer = null; } } /** * 稳定的定时器 */ ScheduledThreadPoolExecutor scheduled; public void timer2(View view) { stopTimer2(); scheduled = new ScheduledThreadPoolExecutor(2); scheduled.scheduleAtFixedRate(new Runnable() { @Override public void run() { loge("time:" + time++); handler.sendEmptyMessage(11); } }, 0, 40, TimeUnit.MILLISECONDS); } public void stop2(View view) { stopTimer2(); } /** * 停止定时器 */ private void stopTimer2() { if (scheduled != null) { scheduled.shutdownNow(); scheduled = null; } } private void loge(String msg) { Log.e("TAG", msg); } @Override protected void onDestroy() { super.onDestroy(); System.exit(0); } /** * 数据清零 */ public void clear(View view) { time = 0; tv_time.setText("" + time); }}
Log数据对比
Timer定时器
11-29 01:08:09.940 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:011-29 01:08:09.980 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:111-29 01:08:10.020 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:211-29 01:08:10.060 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:311-29 01:08:10.100 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:411-29 01:08:10.140 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:511-29 01:08:10.180 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:611-29 01:08:10.224 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:711-29 01:08:10.264 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:811-29 01:08:10.304 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:911-29 01:08:10.344 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1011-29 01:08:10.384 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1111-29 01:08:10.424 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1211-29 01:08:10.468 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1311-29 01:08:10.508 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1411-29 01:08:10.548 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1511-29 01:08:10.592 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1611-29 01:08:10.632 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1711-29 01:08:10.672 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1811-29 01:08:10.712 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1911-29 01:08:10.752 6070-20203/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:20
可以看到上面时间间隔不是很稳定,有几个是间隔40多毫秒的
ScheduledThreadPoolExecutor定时器
11-29 01:11:13.492 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:011-29 01:11:13.528 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:111-29 01:11:13.564 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:211-29 01:11:13.604 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:311-29 01:11:13.644 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:411-29 01:11:13.684 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:511-29 01:11:13.724 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:611-29 01:11:13.764 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:711-29 01:11:13.804 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:811-29 01:11:13.844 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:911-29 01:11:13.888 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1011-29 01:11:13.924 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1111-29 01:11:13.984 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1211-29 01:11:14.004 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1311-29 01:11:14.044 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1411-29 01:11:14.084 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1511-29 01:11:14.124 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1611-29 01:11:14.164 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1711-29 01:11:14.204 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1811-29 01:11:14.244 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:1911-29 01:11:14.284 6070-22987/com.example.wenzhi.scheduledthreadpoolexecutordemo E/TAG: time:20
可以看到上面的数据除了第一个和第二个,其他的数据都是非常稳定的,间隔时间都是40毫秒。
而且时间越长越能看出,这两种定时器的差距。
共勉:当你的野心支撑不了你的梦想的时候,该努力了
- 定时任务ScheduledThreadPoolExecutor的使用详解
- java中使用ScheduledThreadPoolExecutor实现定时任务
- 使用ScheduledThreadPoolExecutor替换Timer执行定时任务
- Java定时任务ScheduledThreadPoolExecutor
- ScheduledThreadPoolExecutor 定时任务demo
- Java定时任务Timer、TimerTask与ScheduledThreadPoolExecutor详解
- 利用ScheduledThreadPoolExecutor定时执行任务
- 利用ScheduledThreadPoolExecutor定时执行任务
- 利用ScheduledThreadPoolExecutor定时执行任务
- 利用ScheduledThreadPoolExecutor定时执行任务
- 利用ScheduledThreadPoolExecutor定时执行任务
- ScheduledThreadPoolExecutor定时使用
- 定时且周期性的任务研究II--ScheduledThreadPoolExecutor
- Java ScheduledThreadPoolExecutor 一个优于Timer、TimerTask的定时任务
- 定时且周期性的任务研究II--ScheduledThreadPoolExecutor
- Java定时任务以及ScheduledThreadPoolExecutor需要注意的问题
- Java 多线程之 ScheduledThreadPoolExecutor 定时任务
- 定时且周期性的任务研究I--Timer ---- 任务研究II--ScheduledThreadPoolExecutor
- Spring项目改成SpringBoot项目
- 11.30考试总结
- 带你逐步深入了解SSH框架——hibernate查询操作
- Coursera Deep Learning 第四课 卷积神经网络 第四周 编程作业Art Generation with Neural Style Transfer
- cuda检验时间
- 定时任务ScheduledThreadPoolExecutor的使用详解
- js-----词法分析过程
- 双向循环链表的插入排序
- VS emulator疑难解答
- dict函数用法
- 11.创新的原则
- Bzoj1208 宠物收养所
- HyperLedger Fabric:自顶向下的方法--第0篇 准备工作
- 洛谷用户协议