关于Android中定时周期执行线程方法
16lz
2021-01-25
主要参数(方法) schedule(Callable callable, long delay, TimeUnit unit)
创建并执行在给定延迟后启用的 ScheduledFuture。
schedule(Runnable command, long delay, TimeUnit unit)
创建并执行在给定延迟后启用的一次性操作。
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnitunit)
创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,TimeUnit unit)
创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。
主要方法 ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor ( 1 );
//周期执行 exec . scheduleAtFixedRate ( new Runnable () {
public void run () {
System . out . println ( format . format ( new Date ()));
}
}, 2000 , 6000 , TimeUnit . MILLISECONDS );
//异常线程 exec.scheduleAtFixedRate(new Runnable() {
public void run() {
System.out.println("RuntimeException no catch,next time can't run");
throw new RuntimeException();
}
}, 2000, 3000, TimeUnit.MILLISECONDS);
注意:如果线程中发生异常就停止周期执行
//延迟线程 exec.scheduleWithFixedDelay(new Runnable() {
public void run() {
System.out.println("延迟线程延迟线程延迟线程,"+format.format(new Date()));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("延迟线程延迟线程,"+format.format(new Date()));
}
},1000,5000,TimeUnit.MILLISECONDS);
//执行一次线程 exec.schedule(new Runnable() {
public void run() {
System.out.println("执行一次线程执行一次线程执行一次线程执行一次线程");
}
},5000,TimeUnit.MILLISECONDS);
}
创建并执行在给定延迟后启用的 ScheduledFuture。
schedule(Runnable command, long delay, TimeUnit unit)
创建并执行在给定延迟后启用的一次性操作。
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnitunit)
创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,TimeUnit unit)
创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。
主要方法 ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor ( 1 );
//周期执行 exec . scheduleAtFixedRate ( new Runnable () {
public void run () {
System . out . println ( format . format ( new Date ()));
}
}, 2000 , 6000 , TimeUnit . MILLISECONDS );
//异常线程 exec.scheduleAtFixedRate(new Runnable() {
public void run() {
System.out.println("RuntimeException no catch,next time can't run");
throw new RuntimeException();
}
}, 2000, 3000, TimeUnit.MILLISECONDS);
注意:如果线程中发生异常就停止周期执行
//延迟线程 exec.scheduleWithFixedDelay(new Runnable() {
public void run() {
System.out.println("延迟线程延迟线程延迟线程,"+format.format(new Date()));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("延迟线程延迟线程,"+format.format(new Date()));
}
},1000,5000,TimeUnit.MILLISECONDS);
//执行一次线程 exec.schedule(new Runnable() {
public void run() {
System.out.println("执行一次线程执行一次线程执行一次线程执行一次线程");
}
},5000,TimeUnit.MILLISECONDS);
}
更多相关文章
- android之fragment与fragment、activity与activity、fragment与a
- android multicast 多播(组播)问题
- Android(安卓)之AnimationDrawable
- Fragment生命周期
- Android网络文件下载模块整理
- Android日历操作
- Android里handler线程间的通信详解
- 深入android数据库操作
- Android源码阅读之Looper