您的位置:首页 > 其它

简单线程系列4-延迟线程池

2012-05-13 19:59 537 查看
代码:

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