模拟打印机排队打印效果
内容摘要
package com.cooly;
import java.util.LinkedList;
/**
* @author coolyqq
*模拟打印打印机排队打印
*分发类
*/
public class DataDistribute {
private static DataD
import java.util.LinkedList;
/**
* @author coolyqq
*模拟打印打印机排队打印
*分发类
*/
public class DataDistribute {
private static DataD
文章正文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | package com.cooly; import java.util.LinkedList; /** * @author coolyqq *模拟打印打印机排队打印 *分发类 */ public class DataDistribute { private static DataDistribute instance = null; private final static byte[] obj = new byte[0]; //锁机制 private LinkedList<DataDistributeEntity> tasks = null; //分发任务 private boolean isColse = true; private DataDistribute() { tasks = new LinkedList<DataDistributeEntity>(); } /** * @return * 获取instance */ public static DataDistribute getInstance(){ if (instance == null){ synchronized (obj) { if (instance == null){ instance = new DataDistribute(); } } } return instance ; } /** * @param entity * 添加任务 */ public void addTask(DataDistributeEntity entity){ synchronized (obj) { tasks.add(entity); } } /** * @param entity * 立即添加任务 */ public void addSpeedTask(DataDistributeEntity entity){ synchronized (obj) { tasks.addFirst(entity); } } public void start(ICallBack callback){ if (tasks==null||tasks.isEmpty()||!this.isColse){ return ; } else { this.isColse = false; } while (true){ DataDistributeEntity entity = tasks.poll(); if (entity==null){ this.isColse = true; break ; } callback.call(entity); tasks.remove(entity); } System.out.println( "fsf" ); } public boolean isColse() { return isColse; } public void setColse(boolean isColse) { this.isColse = isColse; } } |
代码注释