简单线程系列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正在执行。。。
相关文章推荐
- 简单线程系列4-延迟线程池
- c++ 11 多线线程系列-------- 一个最简单而且实用的线程池
- 简单线程系列1-固定大小的线程池
- 简单线程系列3-可变尺寸的线程池
- 简单线程系列1-最简单的线程池
- 关于C#线程,线程池和并行运算的简单使用和对比
- linux网络编程之posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序
- java线程及操作实例,线程池简单例子
- java并发包学习系列:线程复用之线程池(草稿)
- 使用新的java线程池技术创建固定的线程去完成任务,都完成后计算总时间
- [C# 线程处理系列]专题四:线程池中的I/O线程
- [.Net线程处理系列]专题二:线程池中的工作者线程
- C#线程处理系列之线程池中的I/O线程
- java + 线程系列之showThread(一)----简单的线程创建方式有两种
- ThreadPoolTaskExecutor 多线程分解执行耗时任务与单线程串行执行简单比较
- python 线程池 分配任务给线程,爬取加入库。
- [.Net线程处理系列]专题三:线程池中的I/O线程
- c++11 线程池系列之一 最简单的线程池
- 线程池动态并安全地中断所有线程不需要使用shutdownnow()同时可以继续添加任务
- 【学习笔记】多线程编程-线程池/任务/线程