Java 自定义 线程池与任务队列
2016-06-12 11:23
676 查看
说明:
1)使用了队列的先进先出思想
2)在执行的时候添加线程
3)在ThreadQueue 里实现从队列里取出线程
运行结果:
Thread-2 executed OK
Thread-6 executed OK
Thread-1 executed OK
Thread-5 executed OK
Thread-9 executed OK
Thread-7 executed OK
Thread-4 executed OK
Thread-8 executed OK
Thread-3 executed OK
Thread-0 executed OK
Thread-2 executed OK
Thread-5 executed OK
Thread-1 executed OK
Thread-9 executed OK
Thread-7 executed OK
Thread-6 executed OK
Thread-4 executed OK
Thread-8 executed OK
Thread-3 executed OK
Thread-0 executed OK
1)使用了队列的先进先出思想
2)在执行的时候添加线程
3)在ThreadQueue 里实现从队列里取出线程
/** * * @Project JavaDemos * @Package com.java.thread * @author chenlin * @version 1.0 * @Date 2011年6月12日 */ public class WorkQueue { private int threadCounts;// 线程池的大小 private ThreadQueue[] threads;// 用数组实现线程池 private LinkedList queue;// 任务队列 public WorkQueue(int threadCounts) { this.threadCounts = threadCounts; this.threads = new ThreadQueue[threadCounts]; this.queue = new LinkedList(); for (int i = 0; i < threadCounts; i++) { threads[i] = new ThreadQueue(); threads[i].start();// 启动所有工作线程 } } public void execute(Runnable r) { synchronized (queue) { queue.addLast(r); queue.notify(); } } private class ThreadQueue extends Thread {// 工作线程类 @Override public void run() { Runnable r; while (true) { synchronized (queue) { // while (queue.isEmpty()) { try { queue.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } r = (Runnable) queue.removeFirst();// 有任务时,取出任务 } r.run(); } } } /** * 在静态方法里调用内部类必须是静态的 */ static class Mytask implements Runnable {// 任务接口 public void run() { String name = Thread.currentThread().getName(); try { Thread.sleep(100);// 模拟任务执行的时间 } catch (InterruptedException e) { } System.out.println(name + " executed OK"); } } public static void main(String[] args) { WorkQueue wq = new WorkQueue(10);// 10个工作线程 Mytask r[] = new Mytask[20];// 20个任务 for (int i = 0; i < 20; i++) { r[i] = new Mytask(); wq.execute(r[i]); } } }
运行结果:
Thread-2 executed OK
Thread-6 executed OK
Thread-1 executed OK
Thread-5 executed OK
Thread-9 executed OK
Thread-7 executed OK
Thread-4 executed OK
Thread-8 executed OK
Thread-3 executed OK
Thread-0 executed OK
Thread-2 executed OK
Thread-5 executed OK
Thread-1 executed OK
Thread-9 executed OK
Thread-7 executed OK
Thread-6 executed OK
Thread-4 executed OK
Thread-8 executed OK
Thread-3 executed OK
Thread-0 executed OK
相关文章推荐
- 使用Java如何让需求在指定时间内生效
- logic:iterator struts1 功能小例 (转)
- Java框架SSH结合Easyui控件实现省市县三级联动示例解析
- Eclipse快捷键
- spring注解源码分析--how does autowired works?
- javaNIO之选择器
- Eclipse中将一个android项目当做另外一个android项目的library
- Spring注解Annotion详解
- Spring security(一)
- Eclipse中将一个android项目当做另外一个android项目的library
- 关于下拉框切换取值的例子
- eclipse 中spring项目的xml配置打开方式里 找不到Namespaces
- Struts2学习笔记(3)-DMI动态调用方式
- 使用js的encodeURI和encodeURIComponent编码后,java解析乱码
- java中的线程同步
- Eclipse:No fixes avaliable for 'Access restriction: The type basic is not accessible due to restrict
- eclipse启动时jdk环境变量报错解决
- java_Ant详解
- java基础语法练习--输入一串字符统计各字符数目
- spring 泛型API