java队列java中如何实现按队列执行任务

java队列  时间:2021-09-02  阅读:()

java中怎么实现队列

public class Queue<E> { private Object[] data=null; private int maxSize; //队列容量 private int front; //队列头,允许删除 private int rear; //队列尾,允许插入 //构造函数 public Queue(){ this(10); } public Queue(int initialSize){ if(initialSize >=0){ this.maxSize = initialSize; data = new Object[initialSize]; front = rear =0; }else{ throw new RuntimeException("初始化大小不能小于0:" + initialSize); } } //判空 public boolean empty(){ return rear==front?true:false; } //插入 public boolean add(E e){ if(rear== maxSize){ throw new RuntimeException("队列已满,无法插入新的元素!"); }else{ data[rear++]=e; return true; } } //返回队首元素,但不删除 public E peek(){ if(empty()){ throw new RuntimeException("空队列异常!"); }else{ return (E) data[front]; } } //出队 public E poll(){ if(empty()){ throw new RuntimeException("空队列异常!"); }else{ E value = (E) data[front]; //保留队列的front端的元素的值 data[front++] = null; //释放队列的front端的元素 return value; } } //队列长度 public int length(){ return rear-front; } }

java中的队列用什么实现

其内部是用链表实现的。

class AB<T>//一个链表类 { class Node<T>//内部类,表示链表中的一个节点 { Node<T> prev;//前一节点 Node<T> next; //后一节点 T abc; //当前节点的元素 } } 大体如上,具体的功能自行添加。

JAVA编程实现简单的队列入队操作!

class Element{ int id; String name; Element(int a,String n){ id=a;name=n; } } class SeqQueue{ int first,last,maxsize; Element queue[]; SeqQueue(int i){ maxsize=i; first=last=-1; queue=new Element[i]; } public void clear(){//置空 first=last=-1; } public boolean isEmpty(){//判空 if(first==-1)return true; else return false; } public Element getFirst(){//取队列头元素 if(first==-1)return null; else return queue[first+1]; } public boolean isFull(){//判满 if((last+1)%maxsize==first)return true; else return false; } public boolean enQueue(Element e){//入队 if(this.isFull())return false; if(this.isEmpty()) first=last=0; else last=(last+1)%maxsize; queue[last]=e; return true; } public Element deQueue(){//出队 Element t=queue[first]; if(this.isEmpty())return null; if(first==last){ queue[first]=null; this.clear(); return t; } queue[first]=null; first=(first+1)%maxsize; return t; } public int getLength(){//队列长度 if(last>=first)return last-first+1; else return maxsize-(first-last)+1; } public void display(){//打印所有元素 int i,j; for (i=first,j=0;j<this.getLength();i=(i+1)%maxsize,j++) System.out.println(queue[i].id); } }

java中如何实现按队列执行任务

.tone.example; .junit.After; .junit.Before; .junit.Test; .tone.task.TaskProperty; .tone.task.TaskSignature; .tone.task.impl.BasicTask; .tone.task.runner.TaskRunner; /** * 任务队列示例程序 * @author zlf */ public class TaskExample { private TaskRunner taskRunner; /** * 做任务队列的初始化工作 */ @Before public void init() { // 获取任务运行器 taskRunner = TaskRunner.getInstance(); // 将任务运行器放入线程进行调度 Thread thread = new Thread(taskRunner); thread.start(); } /** * 等待任务执行完成,并做最后的退出工作 */ @After public void exit() throws InterruptedException { Thread.sleep(600); System.exit(0); } /** * 最简单的任务运行示例 */ @Test public void example1() { // 添加任务到任务运行器 taskRunner.addTask(new BasicTask() { @Override public void run() { System.out.println("This is running in task runner thread, and thread is " + Thread.currentThread()); } }); } /** * 加入优先执行顺序的任务运行器 */ @Test public void example2() { // 添加任务到任务运行器 taskRunner.addTask(new BasicTask(0) { @Override public void run() { System.out.println("This is a normal task"); } }); taskRunner.addTask(new BasicTask(-1) { @Override public void run() { System.out.println("This is a task a bit high than normal"); } }); } /** * 重复添加的任务只会运行第一个 */ @Test public void example3() { // 添加任务到任务运行器 taskRunner.addTask(new BasicTask(TaskSignature.ONE) { @Override public void run() { System.out.println("This is task one"); } }, TaskProperty.NOT_REPEAT); taskRunner.addTask(new BasicTask(TaskSignature.ONE) { @Override public void run() { System.out.println("This is also task one"); } }, TaskProperty.NOT_REPEAT); } /** * 重复添加的任务只会运行最后一个 */ @Test public void example4() { // 添加任务到任务运行器 taskRunner.addTask(new BasicTask(TaskSignature.ONE) { @Override public void run() { System.out.println("This is task one"); } }, TaskProperty.NOT_REPEAT_OVERRIDE); taskRunner.addTask(new BasicTask(TaskSignature.ONE) { @Override public void run() { System.out.println("This is also task one"); } }, TaskProperty.NOT_REPEAT_OVERRIDE); } }

VoLLcloud7折月付$3,香港CMI云服务器原生IP解锁,香港VoLLcloud

vollcloud怎么样?vollcloud LLC创立于2020年,是一家以互联网基础业务服务为主的 技术型企业,运营全球数据中心业务。VoLLcloud LLC针对新老用户推出全场年付产品7折促销优惠,共30个,机会难得,所有产品支持3日内无条件退款,同时提供产品免费体验。目前所有产品中,“镇店之宝”产品性价比高,适用大部分用户基础应用,卖的也是最好,同时,在这里感谢新老用户的支持和信任,我们...

华纳云新人下单立减40元/香港云服务器月付60元起,香港双向CN2(GIA)

华纳云(HNCloud Limited)是一家专业的全球数据中心基础服务提供商,总部在香港,隶属于香港联合通讯国际有限公司,拥有香港政府颁发的商业登记证明,保证用户的安全性和合规性。 华纳云是APNIC 和 ARIN 会员单位。主要提供数据中心基础服务、互联网业务解决方案, 以及香港服务器租用、香港服务器托管、香港云服务器、美国云服务器,云计算、云安全技术研发等产品和服务。其中云服务器基于成熟的 ...

数脉科技香港自营,10Mbps CN2物理机420元/月

数脉科技怎么样?数脉科技品牌创办于2019,由一家从2012年开始从事idc行业的商家创办,目前主营产品是香港服务器,线路有阿里云线路和自营CN2线路,均为中国大陆直连带宽,适合建站及运行各种负载较高的项目,同时支持人民币、台币、美元等结算,提供支付宝、微信、PayPal付款方式。本次数脉科技给发来了新的7月促销活动,CN2+BGP线路的香港服务器,带宽10m起,配置E3-16G-30M-3IP,...

java队列为你推荐
xhtmlHTML4,HTML5,XHTML 之间有什么区别容灾备份容灾备份的容灾分类表单提交form表单提交能提交页面哪些内容win10发布win10发布到底是中国时间7月29号还是美国时间win10发布win10什么时候发布正式版开发者账号如何免费申请开发者账号antiarpantiARP功能介绍一下招行信用卡还款招商银行信用卡还款日如何计算售后软件有没有什么软件可以接单手机维修gps简介GPS代表什么含义?
免备案虚拟空间 域名买卖 七牛优惠码 raksmart 美国主机网 softbank官网 网站监控 国外空间 警告本网站 主机合租 工信部icp备案号 双线主机 免费申请网站 中国网通测速 linux使用教程 中国电信宽带测速器 免费外链相册 海外空间 脚本大全 酷锐 更多