您的位置:首页 > 其它

简单线程系列5-单任务延迟线程池

2012-05-13 20:02 609 查看
代码:

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class thdd {
public static void main(String[] args) {
ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();
for (int i = 0; i < 100; i++) {
Thread t = new MyThread();
pool.schedule(t, i * 2, TimeUnit.SECONDS);
}
pool.shutdown();
}
}
class MyThread extends Thread {
public void run() {
Date now = new Date();
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
System.out.println(format.format(now) + "\t" + Thread.currentThread().getName() + "正在执行。。。");
}
}


运行结果:

10:35:35    pool-1-thread-1正在执行。。。
10:35:37    pool-1-thread-1正在执行。。。
10:35:39    pool-1-thread-1正在执行。。。
10:35:41    pool-1-thread-1正在执行。。。
10:35:43    pool-1-thread-1正在执行。。。
10:35:45    pool-1-thread-1正在执行。。。
10:35:47    pool-1-thread-1正在执行。。。
10:35:49    pool-1-thread-1正在执行。。。
10:35:51    pool-1-thread-1正在执行。。。
10:35:53    pool-1-thread-1正在执行。。。
10:35:55    pool-1-thread-1正在执行。。。
10:35:57    pool-1-thread-1正在执行。。。
10:35:59    pool-1-thread-1正在执行。。。
10:36:01    pool-1-thread-1正在执行。。。
10:36:03    pool-1-thread-1正在执行。。。
10:36:05    pool-1-thread-1正在执行。。。
10:36:07    pool-1-thread-1正在执行。。。
10:36:09    pool-1-thread-1正在执行。。。
10:36:11    pool-1-thread-1正在执行。。。
10:36:13    pool-1-thread-1正在执行。。。
10:36:15    pool-1-thread-1正在执行。。。
10:36:17    pool-1-thread-1正在执行。。。
10:36:19    pool-1-thread-1正在执行。。。
10:36:21    pool-1-thread-1正在执行。。。
10:36:23    pool-1-thread-1正在执行。。。
10:36:25    pool-1-thread-1正在执行。。。
10:36:27    pool-1-thread-1正在执行。。。
10:36:29    pool-1-thread-1正在执行。。。
10:36:31    pool-1-thread-1正在执行。。。
10:36:33    pool-1-thread-1正在执行。。。
10:36:35    pool-1-thread-1正在执行。。。
10:36:37    pool-1-thread-1正在执行。。。
10:36:39    pool-1-thread-1正在执行。。。
10:36:41    pool-1-thread-1正在执行。。。
10:36:43    pool-1-thread-1正在执行。。。
10:36:45    pool-1-thread-1正在执行。。。
10:36:47    pool-1-thread-1正在执行。。。
10:36:49    pool-1-thread-1正在执行。。。
10:36:51    pool-1-thread-1正在执行。。。
10:36:53    pool-1-thread-1正在执行。。。
10:36:55    pool-1-thread-1正在执行。。。
10:36:57    pool-1-thread-1正在执行。。。
10:36:59    pool-1-thread-1正在执行。。。
10:37:01    pool-1-thread-1正在执行。。。
10:37:03    pool-1-thread-1正在执行。。。
10:37:05    pool-1-thread-1正在执行。。。
10:37:07    pool-1-thread-1正在执行。。。
10:37:09    pool-1-thread-1正在执行。。。
10:37:11    pool-1-thread-1正在执行。。。
10:37:13    pool-1-thread-1正在执行。。。
10:37:15    pool-1-thread-1正在执行。。。
10:37:17    pool-1-thread-1正在执行。。。
10:37:19    pool-1-thread-1正在执行。。。
10:37:21    pool-1-thread-1正在执行。。。
10:37:23    pool-1-thread-1正在执行。。。
10:37:25    pool-1-thread-1正在执行。。。
10:37:27    pool-1-thread-1正在执行。。。
10:37:29    pool-1-thread-1正在执行。。。
10:37:31    pool-1-thread-1正在执行。。。
10:37:33    pool-1-thread-1正在执行。。。
10:37:35    pool-1-thread-1正在执行。。。
10:37:37    pool-1-thread-1正在执行。。。
10:37:39    pool-1-thread-1正在执行。。。
10:37:41    pool-1-thread-1正在执行。。。
10:37:43    pool-1-thread-1正在执行。。。
10:37:45    pool-1-thread-1正在执行。。。
10:37:47    pool-1-thread-1正在执行。。。
10:37:49    pool-1-thread-1正在执行。。。
10:37:51    pool-1-thread-1正在执行。。。
10:37:53    pool-1-thread-1正在执行。。。
10:37:55    pool-1-thread-1正在执行。。。
10:37:57    pool-1-thread-1正在执行。。。
10:37:59    pool-1-thread-1正在执行。。。
10:38:01    pool-1-thread-1正在执行。。。
10:38:03    pool-1-thread-1正在执行。。。
10:38:05    pool-1-thread-1正在执行。。。
10:38:07    pool-1-thread-1正在执行。。。
10:38:09    pool-1-thread-1正在执行。。。
10:38:11    pool-1-thread-1正在执行。。。
10:38:13    pool-1-thread-1正在执行。。。
10:38:15    pool-1-thread-1正在执行。。。
10:38:17    pool-1-thread-1正在执行。。。
10:38:19    pool-1-thread-1正在执行。。。
10:38:21    pool-1-thread-1正在执行。。。
10:38:23    pool-1-thread-1正在执行。。。
10:38:25    pool-1-thread-1正在执行。。。
10:38:27    pool-1-thread-1正在执行。。。
10:38:29    pool-1-thread-1正在执行。。。
10:38:31    pool-1-thread-1正在执行。。。
10:38:33    pool-1-thread-1正在执行。。。
10:38:35    pool-1-thread-1正在执行。。。
10:38:37    pool-1-thread-1正在执行。。。
10:38:39    pool-1-thread-1正在执行。。。
10:38:41    pool-1-thread-1正在执行。。。
10:38:43    pool-1-thread-1正在执行。。。
10:38:45    pool-1-thread-1正在执行。。。
10:38:47    pool-1-thread-1正在执行。。。
10:38:49    pool-1-thread-1正在执行。。。
10:38:51    pool-1-thread-1正在执行。。。
10:38:53    pool-1-thread-1正在执行。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: