行列处理
队列处理
在做项目的时候有这个需求:用户提交请求需要排队处理(先来先处理,后来请求处于等待状态),用户可以查看所有请求状态。
序号 | 用户名 | 提交类型 | 提交时间 | 开始处理时间 | 处理结束时间 | 处理状态 |
4 | 张三 | aaaa | 2011-11-16 17:14:34 | waiting | ||
3 | 李四 | sss | 2011-11-16 17:14:09 | 2011-11-16 17:14:09 | process | |
2 | 王五 | dddd | 2011-11-16 17:13:47 | 2011-11-16 17:13:47 | 2011-11-16 17:13:57 | failed |
1 | 赵六 | ffff | 2011-11-16 17:13:34 | 011-11-16 17:13:34 | 2011-11-16 17:13:44 | success |
pm设计的方案是:用两个对列解决。一个队列处理所有请求的状态,将状态置为waiting。第二个队列处理请求。这样就可以在列表中展示所有请求状态,又可以处理请求。
我对此不懂,现在权当一个处理方法记录下来。至于此方法是否ok,我就不敢保证。只当一个解决方法积累。
<!--EndFragment-->
Object object = null; // object在这里设置了下其他状态 // 这个队列存放等待的所有对象 MyQueueTwo bMyQueueTwo = MyQueueTwo.getInstance(); bMyQueueTwo.queueTwo.offer(object); // 将userData对象放入queue中(队列中) MyQueue aMyQueue = MyQueue.getInstance(); aMyQueue.queue.offer(object); // 对象 Object str = null; Object strTwo = null; // 第一个队列————设置所有请求状态 while ((strTwo = bMyQueueTwo.queueTwo.poll()) != null) { // 语句块 } // 从队列中一个一个取出来(先进先出),处理请求 synchronized (aMyQueue) { while ((str = aMyQueue.queue.peek()) != null) { // 处理异常,有异常时需要将队头的对象释放掉,才会处理下一个请求 try { // 语句块 } catch (Exception e2) { aMyQueue.queue.poll(); e2.printStackTrace(); continue; } // for循环中释放对象 for (int i = 0; i < strPatchFilePathaArray.length; i++) { try { } catch (Exception e) { aMyQueue.queue.poll(); e.printStackTrace(); break; } } } }
public class MyQueue { public static Queue<Object> queue = null; private static MyQueue instance = null; private MyQueue() { queue = new LinkedList<Object>(); } public static synchronized MyQueue getInstance() { if (instance == null) { instance = new MyQueue(); } return instance; } }