简单线程系列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正在执行。。。
相关文章推荐
- 简单线程系列5-单任务延迟线程池
- c++ 11 多线线程系列-------- 一个最简单而且实用的线程池
- 简单线程系列1-固定大小的线程池
- 简单线程系列1-最简单的线程池
- 简单线程系列3-可变尺寸的线程池
- 重新想象 Windows 8 Store Apps (42) - 多线程之线程池: 延迟执行, 周期执行, 在线程池中找一个线程去执行指定的方法
- [.Net线程处理系列]专题三:线程池中的I/O线程
- [.Net 多线程处理系列]专题二:线程池中的工作者线程
- posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序
- Java线程池及Future、Callable获得线程返回结果【Java线程池系列2】
- [.Net 多线程处理系列]专题三:线程池中的I/O线程
- java线程及操作实例,线程池简单例子
- 关于C#线程,线程池和并行运算的简单使用和对比
- [.Net线程处理系列]专题二:线程池中的工作者线程
- 一个简单的线程池工具类——可以实现对单个线程的控制
- C#线程系列讲座(3):线程池和文件下载服务器
- 线程模型、pthread 系列函数 和 简单多线程服务器端程序
- 线程 —— 线程池简单介绍
- [.Net线程处理系列]专题三:线程池中的I/O线程
- C#线程处理系列之线程池中的I/O线程