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); } }

云基最高500G DDoS无视CC攻击(Yunbase),洛杉矶CN2GIA、国内外高防服务器

云基成立于2020年,目前主要提供高防海内外独立服务器用户,欢迎各类追求稳定和高防优质线路的用户。业务可选:洛杉矶CN2-GIA+高防(默认500G高防)、洛杉矶CN2-GIA(默认带50Gbps防御)、香港CN2-GIA高防(双向CN2GIA专线,突发带宽支持,15G-20G DDoS防御,无视CC)、国内高防服务器(广州移动、北京多线、石家庄BGP、保定联通、扬州BGP、厦门BGP、厦门电信、...

Dynadot COM特价新注册48元

想必我们有一些朋友应该陆续收到国内和国外的域名注册商关于域名即将涨价的信息。大概的意思是说从9月1日开始,.COM域名会涨价一点点,大约需要单个9.99美元左右一个。其实对于大部分用户来说也没多大的影响,毕竟如今什么都涨价,域名涨一点点也不要紧。如果是域名较多的话,确实增加续费成本和注册成本。今天整理看到Dynadot有发布新的八月份域名优惠活动,.COM首年注册依然是仅需48元,本次优惠活动截止...

特网云(198元/月),高质量云虚拟主机低至0.16元/天,裸金属服务器仅需10.5元/天

特网云为您提供高速、稳定、安全、弹性的云计算服务计算、存储、监控、安全,完善的云产品满足您的一切所需,深耕云计算领域10余年;我们拥有前沿的核心技术,始终致力于为政府机构、企业组织和个人开发者提供稳定、安全、可靠、高性价比的云计算产品与服务。官方网站:https://www.56dr.com/ 10年老品牌 值得信赖 有需要的请联系======================特网云推出多IP云主机...

java队列为你推荐
range英雄联盟out of range怎么调过来起英文名好听的英文名字体系文件什么叫采购体系文件短信应用默认短信应用,什么意思?antiarp360防火墙:antiarp.exe文件损坏.运行chkdsk是什么意思?怎么处理?网络图绘制双代号网络图的绘制方法?burndown如何画burndown chart主板说明书电脑主板的作用是什么?360官网打不开360系统防护无法开启?怎么办?急!!!!!无线互联网WiFi没有连接到互联网是什么意思
域名查询软件 泛域名绑定 国内免备案主机 电影服务器 pccw 国外idc 精品网 圣诞促销 vip购优汇 789电视网 免费吧 泉州移动 1g内存 服务器合租 河南移动m值兑换 免费asp空间 德隆中文网 万网注册 乐视会员免费领取 双十二促销 更多