模仿打印機列隊打印後果。本站提示廣大學習愛好者:(模仿打印機列隊打印後果)文章只能為提供參考,不一定能成為您想要的結果。以下是模仿打印機列隊打印後果正文
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; } }