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

SoftShellWeb:台湾(台北)VPS年付49美元起,荷兰VPS年付24美元起

SoftShellWeb是一家2019年成立的国外主机商,商家在英格兰注册,提供的产品包括虚拟主机和VPS,其中VPS基于KVM架构,采用SSD硬盘,提供IPv4+IPv6,可选美国(圣何塞)、荷兰(阿姆斯特丹)和台湾(台北)等机房。商家近期推出台湾和荷兰年付特价VPS主机,其中台湾VPS最低年付49美元,荷兰VPS年付24美元起。台湾VPSCPU:1core内存:2GB硬盘:20GB SSD流量...

DMIT:美国cn2 gia线路vps,高性能 AMD EPYC/不限流量(Premium Unmetered),$179.99/月起

DMIT怎么样?DMIT最近动作频繁,前几天刚刚上架了日本lite版VPS,正在酝酿上线日本高级网络VPS,又差不多在同一时间推出了美国cn2 gia线路不限流量的美国云服务器,不过价格太过昂贵。丐版只有30M带宽,月付179.99美元 !!目前,美国云服务器已经有个4个套餐,分别是,Premium(cn2 gia线路)、Lite(普通直连)、Premium Secure(带高防的cn2 gia线...

CloudCone中国新年特别套餐,洛杉矶1G内存VPS年付13.5美元起

CloudCone针对中国农历新年推出了几款特别套餐, 其中2019年前注册的用户可以以13.5美元/年的价格购买一款1G内存特价套餐,以及另外提供了两款不限制注册时间的用户可购买年付套餐。CloudCone是Quadcone旗下成立于2017年的子品牌,提供VPS及独立服务器租用,也是较早提供按小时计费VPS的商家之一,支持使用PayPal或者支付宝等付款方式。下面列出几款特别套餐配置信息。CP...

java队列为你推荐
expireredis expire 命令有什么好处CAA什么叫CAA脑溢血?java队列怎样用java代码实现一个队列起英文名好听的英文名字0x800ccc0f任务“POP3.163.COM-正在接收”报告了错误(0x800CCC0F)网络购物的发展网购发展史sd卡座SD卡座 FPC座子请问这些产品哪家厂家的货比较好啊?sd卡座SD卡座有能满足CE认证的吗?淘宝推广网站谁有好的淘宝推广网站介绍下哦!或推广技巧!安全工程师待遇安全工程就业的待遇怎么样,
电影服务器 Hello图床 iis安装教程 win8.1企业版升级win10 国外在线代理 百兆独享 怎样建立邮箱 idc资讯 admit的用法 已备案删除域名 nerds 服务器是干什么的 美国在线代理服务器 百度云1t hkt web服务器安全 江苏双线服务器 新睿云 万网主机管理 空间登入 更多