Java线程调度ScheduledThreadPoolExecutor简单使用样例

来源:互联网 发布:手机签名设计软件 编辑:程序博客网 时间:2024/05/29 11:08

Java线程调度ScheduledThreadPoolExecutor简单使用样例


代码例子:

package test;import java.util.concurrent.ScheduledThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class Test {private static class TestTask implements Runnable {private String TAG = "";public TestTask(String tag) {TAG = tag;}@Overridepublic void run() {System.out.println(TAG + "\t" + System.currentTimeMillis());}}public static void main(String[] args) {ScheduledThreadPoolExecutor mScheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(2);int time = 3; // 延迟3秒执行TestTask zhang = new TestTask("zhang");TestTask phil = new TestTask("phil");mScheduledThreadPoolExecutor.schedule(zhang, time, TimeUnit.SECONDS);// 再上一个任务的3秒后执行mScheduledThreadPoolExecutor.schedule(phil, time * 2, TimeUnit.SECONDS);}}

代码运行结果输出:

zhang1488963240889phil1488963243889


1 0
原创粉丝点击