欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

模拟打印机排队打印效果

程序员文章站 2024-02-29 19:15:22
package com.cooly; import java.util.linkedlist; /** * @author coolyqq *模拟打...
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;
}
}