模擬打印機排隊打印效果
更新時間:2014年07月31日 09:54:10 投稿:whsnow
本節(jié)主要介紹了模擬打印機排隊打印效果的具體實現(xiàn),感興趣的朋友可以參考下
package com.cooly;
import java.util.LinkedList;
/**
* @author coolyqq
*模擬打印打印機排隊打印
*分發(fā)類
*/
public class DataDistribute {
private static DataDistribute instance = null;
private final static byte[] obj = new byte[0];//鎖機制
private LinkedList<DataDistributeEntity> tasks = null;//分發(fā)任務(wù)
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
* 添加任務(wù)
*/
public void addTask(DataDistributeEntity entity){
synchronized (obj) {
tasks.add(entity);
}
}
/**
* @param entity
* 立即添加任務(wù)
*/
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;
}
}
相關(guān)文章
詳解Spring-Cloud2.0之Feign調(diào)用遠程服務(wù)指南
這篇文章主要介紹了詳解Spring-Cloud2.0之Feign調(diào)用遠程服務(wù)指南,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
springboot使用定時器@Scheduled不管用的解決
這篇文章主要介紹了springboot使用定時器@Scheduled不管用的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

