非阻塞队列的实现

硅谷探秘者 1521 0 0

基于 双向链表结构

直接代码:

package threadTest.test6;

public class Node<E> {
	private E e;
	private Node<E> prve;
	private Node<E> next;
	public Node(E e) {
		super();
		this.e = e;
	}
	public E getE() {
		return e;
	}
	public void setE(E e) {
		this.e = e;
	}
	public Node<E> getPrve() {
		return prve;
	}
	public void setPrve(Node<E> prve) {
		this.prve = prve;
	}
	public Node() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Node<E> getNext() {
		return next;
	}
	public void setNext(Node<E> next) {
		this.next = next;
	}
	@Override
	protected void finalize() throws Throwable {
		System.out.println("当前对象被释放:"+e);
	}
}
package threadTest.test6;
public class Queue<E> {
	private Node<E> head;//队首
	private Node<E> tail;//队尾
	private Integer size;//队列长度
	private Integer s;//当前队列长度
	/**
	 * 定义队列最大长度 size
	 * @param size
	 */
	public Queue(Integer size) {
		super();
		head = new Node<E>();
		tail = new Node<E>();
		head.setNext(tail);
		head.setPrve(null);
		tail.setNext(null);
		tail.setPrve(head);
		this.size = size;
		this.s=0;
	}
	/**
	 * 默认队列长度为:Integer.MAX_VALUE
	 */
	public Queue() {
		super();
		head = new Node<E>();
		tail = new Node<E>();
		head.setNext(tail);
		head.setPrve(null);
		tail.setNext(null);
		tail.setPrve(head);
		this.size=Integer.MAX_VALUE;
		this.s=0;
	}
	/**
	 * 入队操作
	 * @param e
	 */
	public void push(E e) {
		if(this.s>=this.size) {
			System.err.println("队列已满,入队失败");
			return;
		}
		Node<E> n=new Node<E>(e);
		n.setPrve(tail.getPrve());
		n.setNext(tail);
		tail.getPrve().setNext(n);
		tail.setPrve(n);
		this.s++;
	}
	/**
	 * 出队操作
	 * @return
	 */
	public E pop() {
		if(this.s<=0) {
			System.err.println("空队列");
			return null;
		}
		Node<E> n=head.getNext();
		n.getNext().setPrve(head);
		head.setNext(n.getNext());
		s--;
		return n.getE();
	}
	@Override
	public String toString() {
		Node<E> no=head.getNext();
		while(no!=tail) {
			System.out.println(no.getE());
			no=no.getNext();
		}
		return "";
	}
}
package threadTest.test6;

public class TestMain {
	public static void main(String[] args) {
		Queue<Integer> q=new Queue<>(3);
		q.push(1);
		q.push(2);
		q.push(3);
		q.push(4);
		q.push(5);
		q.push(6);
		System.out.println(q.pop());
		System.out.println(q.pop());
		System.out.println(q.pop());
		System.out.println(q.pop());
	}
}

image.png



评论区
请写下您的评论...
暂无评论...
猜你喜欢
数据结构与算法 1336 ,放入后台一个执行中,后台可以慢慢执行,当中没有业务数据时,使该执行线程进入等待状态。当业务数据添加进中后唤醒处于等待状态执行线程,继续处理业务。一、packagecom.
java基础 4177 变为空。2.应用场景:常用于生产者和消费者场景,生产者是向里添加元素线程,消费者是从里取元素线程。简而言之,是生产者用来存放元素、消费者获取元素容器。3
official 989 之前文章中提到了java中nio是同步网络io模型,本文就主要说明一下同步、异步、概念来帮助理解nio。io操作IO分两阶段(一旦拿到数据后就变成了数据操作,不再是IO
数据结构与算法 4871 ,largestout)行为特征。通常采用堆数据结构来。优先级是不同于先进先出另一种。每次从中取出是具有最高优先权元素。操作:1.往中添加数据2.从中获取数据优先级
weblog 3064 了解优先级详细叙述请访问(java):java用数组优先级(小顶堆) 验目: 先按key优先,如果key值相等再按value.hg优先。 插入数据案例
official 876 上一篇《(mq)rabbitmq安装延时插件延时消息1》文章中介绍了rabbitmq安装延时插件。本编将继续结合代码来延时(基于springboot项目)。下方所有源代码均已上传
official 897 态→就绪态:需修改PCB内容和相应就绪态→运行态:需恢复进程运行环境、修改PCB内容和相应运行态→态:保存进程运行环境,修改PCB内容和相应态→就绪态:需修改PCB内容和相应。果
数据结构与算法 2379 链式基本操作c++classnode{public:intdata;node*next;node*prev;};#include"node.h"classqueue{private:node
归档
2018-11  12 2018-12  33 2019-01  28 2019-02  28 2019-03  32 2019-04  27 2019-05  33 2019-06  6 2019-07  12 2019-08  12 2019-09  21 2019-10  8 2019-11  15 2019-12  25 2020-01  9 2020-02  5 2020-03  16 2020-04  4 2020-06  1 2020-07  7 2020-08  13 2020-09  9 2020-10  5 2020-12  3 2021-01  1 2021-02  5 2021-03  7 2021-04  4 2021-05  4 2021-06  1 2021-07  7 2021-08  2 2021-09  8 2021-10  9 2021-11  16 2021-12  14 2022-01  7 2022-05  1 2022-08  3 2022-09  2 2022-10  2 2022-12  5 2023-01  3 2023-02  1 2023-03  4 2023-04  2 2023-06  3 2023-07  4 2023-08  1 2023-10  1 2024-02  1 2024-03  1 2024-04  1
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。