一个多线程顺序执行的Queue模型
来源:互联网 发布:bearychat mac 编辑:程序博客网 时间:2024/05/16 01:39
package cn.test.main;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;public class HandleQueue { boolean done = false; private final BlockingQueue<Action> queue; ExecutorService es ; int counter = 0; public HandleQueue() {super();queue = new ArrayBlockingQueue<Action>(500, true);es = Executors.newSingleThreadExecutor();} class TestCallback implements Callable<Boolean>{ private Action action; public TestCallback(Action action) { this.action = action; } @Overridepublic Boolean call() throws Exception {// TODO Auto-generated method stub System.out.println( "TaskId-"+action.id);for(int i = 0 ; i<10;i++){ System.out.println(Thread.currentThread().getName()+"call step-"+i); Thread.sleep(500);}return true;} } public void init(){ Thread t = new Thread() { public void run() { while (!done){ Action action = nextAction(); if (action != null) { counter++; Future <Boolean> future = es.submit(new TestCallback(new Action(counter))); try {if( future.get()){ System.out.println("执行成功"); }} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ExecutionException e) {// TODO Auto-generated catch blocke.printStackTrace();} if (queue.isEmpty()) { } } } } }; t.start();} public void putAction(Action action) { if (!done) { try { System.out.println("放入Action"); queue.put(action); } catch (InterruptedException ie) { ie.printStackTrace(); return; } synchronized (queue) { queue.notifyAll(); } } } private Action nextAction() { Action a= null; // Wait until there's a Action or we're done. while (!done && (a = queue.poll()) == null) { try { synchronized (queue) { queue.wait(); } } catch (InterruptedException ie) { ie.printStackTrace(); // Do nothing } } return a; }/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubHandleQueue q = new HandleQueue(); q.init();for(int i = 0 ; i<10 ;i++){q.putAction(new Action(i));}}}
0 0
- 一个多线程顺序执行的Queue模型
- 多线程的顺序执行(1)
- 多线程的顺序执行(2)
- J2SE多线程 part2 多线程的执行顺序
- OC中多线程执行顺序的控制
- Java多线程顺序执行
- 多线程执行顺序
- 多线程执行顺序
- 多线程执行顺序
- 多线程执行顺序、速度
- Queue的顺序实现
- 一个判断Java 执行顺序的例子
- netty的Pipeline里面的执行定制模型顺序问题
- 一个线程调度的多线程模型
- Linux c多线程执行顺序
- java控制多线程执行顺序
- java控制多线程执行顺序
- Java控制多线程执行顺序
- 安卓开发之相对布局
- 多队列网卡简介
- C++第二次作业
- 第三周任务
- ArcGis for flex 中QueryTask查询结果限制(1000)问题
- 一个多线程顺序执行的Queue模型
- First Missing Positive
- Java基础__银行调度系统
- php 这样写日志 sleep你会了吗?
- 死锁的定义及产生死锁的四个条件
- 关于用visio 2007画用例图时没有include关系的解决方法
- window.showModalDialog返回值
- MySQL 存储过程调试工具商业和免费
- Configuring HugePages for Oracle onLinux