java的dequeue的简单介绍

博主:adminadmin 2023-01-28 20:48:09 328

本篇文章给大家谈谈java的dequeue,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 中 阻塞队列 非阻塞队列 和普通队列的区别是什么?

阻塞队列与普通队列的区别在于,当队列是空的时,从队列中获取元素的操作将会被阻塞,或者当队列是满时,往队列里添加元素的操作会被阻塞。试图从空的阻塞队列中获取元素的线程将会被阻塞,直到其他的线程往空的队列插入新的元素。同样,试图往已满的阻塞队列中添加新元素的线程同样也会被阻塞,直到其他的线程使队列重新变得空闲起来,如从队列中移除一个或者多个元素,或者完全清空队列.

从5.0开始,JDK在java.util.concurrent包里提供了阻塞队列的官方实现。尽管JDK中已经包含了阻塞队列的官方实现,但是熟悉其背后的原理还是很有帮助的。一下是阻塞队列的实现:

public class BlockingQueue {

private List queue = new LinkedList();

private int  limit = 10;

public BlockingQueue(int limit){

this.limit = limit;

}

public synchronized void enqueue(Object item)

throws InterruptedException  {

while(this.queue.size() == this.limit) {

wait();

}

if(this.queue.size() == 0) {

notifyAll();

}

this.queue.add(item);

}

public synchronized Object dequeue()

throws InterruptedException{

while(this.queue.size() == 0){

wait();

}

if(this.queue.size() == this.limit){

notifyAll();

}

return this.queue.remove(0);

}

}

java中的queue类是什么,啥作用?

你好,先理解下:

在计算机学科中,基础数据结构之一 — Queue。你会想起Queue是一种数据结构,在它里边的元素可以按照添加它们的相同顺序被移除。在以前的Java版本中,这中FIFO(先进先出)数 据结构很不幸被忽略了。随着Java1.5(也叫Tiger)的出现,对Queue支持第一次成为固有特性。

例子核心代码:

QueueString myQueue = new LinkedListString();

myQueue.add("Add Me");

myQueue.add("Add Me Too");

String enqueued = myQueue.remove();

前面的定义才是重点,剩下的与集合类差不多。

java 队列

//通过LinkedList实现队列

package 队列和堆栈;

import java.util.*;

public class LinkedListQueueTest {

//字段

private LinkedList list;

//无参数构造

public LinkedListQueueTest()

{

list=new LinkedList();

}

//队列元素的个数

public int size()

{

return list.size();

}

//进入队列

public void enqueue(Object obj)

{

list.addLast(obj);

}

//对头出来

public Object dequeue()

{

return list.removeFirst();

}

//浏览对头元素

public Object front()

{

//return list.getFirst();

return list.peekFirst();

}

//判断队列是否为空

public boolean isEmpty()

{

return list.isEmpty();

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

LinkedListQueueTest llq=new LinkedListQueueTest();

System.out.println(llq.isEmpty());

llq.enqueue("147");

llq.enqueue("258");

llq.enqueue("369");

System.out.println(llq.size());

System.out.println("移除队列头元素:"+llq.dequeue());

System.out.println(llq.size());

llq.enqueue("abc");

llq.enqueue("def");

System.out.println(llq.size());

System.out.println("查看队列的头元素:"+llq.front());

System.out.println(llq.size());

System.out.println(llq.isEmpty());

}

}

通过数组实现

package 队列和堆栈;

import java.util.NoSuchElementException;

//通过数组来实现队列

public class ArrayQueue {

//字段

public static Object[] data;

//队列的元素个数

protected int size ;

//队列头

protected int head;

//队列尾

public static int tail;

/**

*

*/

//无参数构造函数

public ArrayQueue() {

final int INITIAL_LENGTH=3;

data=new Object[INITIAL_LENGTH];

size=0;

head=0;

tail=-1;

}

//队列元素个数方法

public int size()

{

return size;

}

public boolean isEmpty()

{

return size==0;

}

//得到队列头元素

public Object front()

{

if(size==0)

throw new NoSuchElementException();

return data[head];

}

//进入队列enqueue()方法

public void enqueue(Object obj)

{

//此时队列已经满

if(size==data.length){

Object[] oldData=data;

data=new Object[data.length*2];

//if(head==0)

System.arraycopy(oldData, head, data, 0, oldData.length-head);

if(head0)

System.arraycopy(oldData, 0, data, head+1, tail+1);

head=0;

tail=oldData.length-1;

}

tail=(tail+1)%data.length;

size++;

data[tail]=obj;

}

//队列的元素出队

public Object dequeue()

{

if(size==0)

throw new NoSuchElementException();

Object ele=data[head];

//循环队列

head=(head+1)%data.length;

size--;

return ele;

}

@Override

public String toString() {

// TODO Auto-generated method stub

return super.toString();

}

}

通过向量实现:

//通过向量实现栈

package 队列和堆栈;

import java.util.*;

public class VectorStackTest {

//字段

Vector v;

//构造函数

public VectorStackTest()

{

v=new Vector();

}

//元素的个数

public int size()

{

return v.size();

}

//是否为空

public boolean isEmpty()

{

return size()==0;

}

//进栈

public Object Push(Object obj)

{

v.addElement(obj);

return obj;

}

//出栈方法

public Object Pop()

{

int len=size();

Object obj=Peek();

v.removeElementAt(len-1);

return obj;

}

//查看栈顶元素

public Object Peek()

{

int len = size();

if (len == 0)

throw new EmptyStackException();

return v.elementAt(len - 1);

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

VectorStackTest vst=new VectorStackTest();

System.out.println("大小:"+vst.size());

vst.Push("123");

vst.Push("456");

vst.Push("789");

vst.Push("abc");

System.out.println("大小:"+vst.size());

System.out.println("栈顶:"+vst.Peek());

System.out.println("出栈:"+vst.Pop());

vst.Push("def");

vst.Push("147");

System.out.println("大小:"+vst.size());

System.out.println("栈顶:"+vst.Peek());

System.out.println("出栈:"+vst.Pop());

System.out.println(vst.Peek());

vst.Push("def");

vst.Push("147");

System.out.println(vst.Pop());

System.out.println(vst.Pop());

System.out.println(vst.Peek());

System.out.println(vst.Pop());

System.out.println(vst.Pop());

vst.Push("1aadf");

vst.Push("2dafad");

vst.Push("123789");

System.out.println(vst.Pop());

System.out.println(vst.Peek());

System.out.println(vst.Pop());

System.out.println(vst.Peek());

System.out.println("------------------end------------");

VectorStackTest llst=new VectorStackTest();

llst.Push("123");

llst.Push("456");

System.out.println("栈顶:"+llst.Peek());

System.out.println("出栈:"+llst.Pop());

System.out.println(llst.Peek());

llst.Push("789");

llst.Push("abc");

System.out.println("栈顶:"+llst.Peek());

System.out.println("出栈:"+llst.Pop());

System.out.println(llst.size());

System.out.println("栈顶:"+llst.Peek());

}

}

推荐:都看API文档。有疑问可以问我,QQ:285479197

java多线程解决同步问题的几种方式,原理和代码

在Java中一共有四种方法支持同步,其中前三个是同步方法,一个是管道方法。管道方法不建议使用。

wait()/notify()方法

await()/signal()方法

BlockingQueue阻塞队列方法

PipedInputStream/PipedOutputStream

阻塞队列的一个简单实现:

public class BlockingQueue {

 private List queue = new LinkedList();

 private int  limit = 10;

 public BlockingQueue(int limit){

   this.limit = limit;

 }

 public synchronized void enqueue(Object item)throws InterruptedException  {

   while(this.queue.size() == this.limit) {

     wait();

   }

   if(this.queue.size() == 0) {

     notifyAll();

   }

   this.queue.add(item);

 }

 public synchronized Object dequeue()  throws InterruptedException{

   while(this.queue.size() == 0){

     wait();

   }

   if(this.queue.size() == this.limit){

     notifyAll();

   }

   return this.queue.remove(0);

 }}

在enqueue和dequeue方法内部,只有队列的大小等于上限(limit)或者下限(0)时,才调用notifyAll方法。如果队列的大小既不等于上限,也不等于下限,任何线程调用enqueue或者dequeue方法时,都不会阻塞,都能够正常的往队列中添加或者移除元素。

wait()/notify()方法

生产者的主要作用是生成一定量的数据放到缓冲区中,然后重复此过程。与此同时,消费者也在缓冲区消耗这些数据。该问题的关键就是要保证生产者不会在缓冲区满时加入数据,消费者也不会在缓冲区中空时消耗数据。

要解决该问题,就必须让生产者在缓冲区满时休眠(要么干脆就放弃数据),等到下次消费者消耗缓冲区中的数据的时候,生产者才能被唤醒,开始往缓冲区添加数据。同样,也可以让消费者在缓冲区空时进入休眠,等到生产者往缓冲区添加数据之后,再唤醒消费者。

Java设计一个名为Queue的类用于存储整数。像栈一样,队列具有元素。在栈中,元素以“后进先出”的方式

参考代码和注释如下

简单测试了下,如果有问题接着在讨论

public class Queue {

private int[] elements;

public static final int DEFAULT_CAPACITY =8;//默认长度8

private int size = 0;

public Queue() {

elements = new int[DEFAULT_CAPACITY];

}

public Queue(int capacity) {

elements = new int[capacity];

}

public void enqueue(int v) {

if (size = elements.length) {// 数组扩容

int[] temp = new int[elements.length * 2];

System.arraycopy(elements, 0, temp, 0, elements.length);

elements = temp;

}

elements[size++] = v;

}

public int dequeue() {// 先进先出

if (empty()) {

throw new RuntimeException("异常");

}

int x = elements[0];// 先把第一个元素保存出来

// 左移一位

// int[] temp = new int[elements.length];

// System.arraycopy(elements,1, temp, 0, elements.length-1);

// elements = temp;

// 左移一位

for (int i = 0; i  elements.length - 1; i++) {

elements[i] = elements[i + 1];

}

elements[elements.length - 1] = 0;// 外面一般访问不了elements 后面的元素可以不用归零,但是归零了感觉舒服点

size--;

return x;

}

public boolean empty() {

return size == 0;

}

public int getSize() {

return size;

}

}

输出

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

java的dequeue的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java的dequeue的信息别忘了在本站进行查找喔。