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

2022年最新PHP短网址生成系统/短链接生成系统/URL缩短器系统源码

全新PHP短网址系统URL缩短器平台,它使您可以轻松地缩短链接,根据受众群体的位置或平台来定位受众,并为缩短的链接提供分析见解。系统使用了Laravel框架编写,前后台双语言使用,可以设置多域名,还可以开设套餐等诸多功能,值得使用。链接: https://pan.baidu.com/s/1ti6XqJ22tp1ULTJw7kYHog?pwd=sarg 提取码: sarg文件解压密码 www.wn7...

Spinservers:美国圣何塞服务器,双E5/64GB DDR4/2TB SSD/10Gbps端口月流量10TB,$111/月

spinservers怎么样?spinservers大硬盘服务器。Spinservers刚刚在美国圣何塞机房补货120台独立服务器,CPU都是双E5系列,64-512GB DDR4内存,超大SSD或NVMe存储,数量有限,机器都是预部署好的,下单即可上架,无需人工干预,有需要的朋友抓紧下单哦。Spinservers是Majestic Hosting Solutions,LLC旗下站点,主营美国独立...

CloudCone:KVM月付1.99美元起,洛杉矶机房,支持PayPal/支付宝

CloudCone的[2021 Flash Sale]活动仍在继续,针对独立服务器、VPS或者Hosted email,其中VPS主机基于KVM架构,最低每月1.99美元,支持7天退款到账户,可使用PayPal或者支付宝付款,先充值后下单的方式。这是一家成立于2017年的国外VPS主机商,提供独立服务器租用和VPS主机,其中VPS基于KVM架构,多个不同系列,也经常提供一些促销套餐,数据中心在洛杉...

java队列为你推荐
重装系统后数据恢复电脑重装后数据能恢复吗descriptionDescription是什么意思?fast路由器FAST无线路由器要怎么设置swift语言swift语言坑死ios开发者,这样怎么说呢?有基础入门课程推荐吗?什么是cookie有时遇到网页提示禁止第三方cookie是什么意思?空间背景图片怎么设置空间背景图片4g上网卡什么是4G无线上网卡站内搜索引擎站内搜索与百度的搜索引擎有什么本质性的区别?哪些大数据公司提供站内搜索这种服务?摇一摇周边公众号怎么用微信摇一摇周边功能趋势防毒如何给服务器安装趋势防毒网络版软件
cn域名注册 个人域名备案流程 pw域名 isatap 好看的留言 云图标 godaddy域名证书 百兆独享 php空间购买 hktv 华为云服务登录 smtp虚拟服务器 中国电信测速器 网购分享 帽子云排名 东莞主机托管 摩尔庄园注册 xuni 攻击服务器 网站防护 更多