自己写了个简单的实现
class QueueE{
private Object[] integerQueue;//用来当队列
public int tail;//队尾
public int size;//队的长度,也可以设置一个默认值,溢出时从新申请
public Queue(int size){
integerQueue=new Object[size];
this.size=size;
tail=-1;
}
/**
* 将元素插入队列
* @return 如果该元素已添加到此队列,则返回 true;否则返回 false
*/
public boolean offer(E e){
if(tail size-1){
tail++;
this.integerQueue[tail]=e;
return true;
}else{
return false;
* 获取并移除此队列的头,如果此队列为空,则返回 null.
public E poll(){
Object tmp;
if(tail=0){
tmp=this.integerQueue[tail];
tail--;
return (E)tmp;
return null;
①1. 队列的数据结构
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为队尾,进行删除操作的端称为队头.
QueueTest
public class QueueTest {
public static void main(String[] args) {
ArrayQueue queue = new ArrayQueue(10);
System.out.println(queue.isEmpty());
for (int i = 0; i 10; i++) {
queue.insert(i);
System.out.println(queue.isFull());
while (!queue.isEmpty()) {
System.out.println(queue.remove());
class ArrayQueue {
private int[] arrInt;// 内置数组
private int front;// 头指针
private int rear;// 尾指针
public ArrayQueue(int size) {
this.arrInt = new int[size];
front = 0;
rear = -1;
* 判断队列是否为空
*
* @return
public boolean isEmpty() {
return front == arrInt.length;
* 判断队列是否已满
public boolean isFull() {
return arrInt.length - 1 == rear;
* 向队列的队尾插入一个元素
public void insert(int item) {
if (isFull()) {
throw new RuntimeException("队列已满");
arrInt[++rear] = item;
* 获得对头元素
public int peekFront() {
return arrInt[front];
* 获得队尾元素
public int peekRear() {
return arrInt[rear];
* 从队列的对头移除一个元素
public int remove() {
if (isEmpty()) {
throw new RuntimeException("队列为空");
return arrInt[front++];
运行结果如下:
false
true
java中的消息队列
消息队列是线程间通讯的手段:
import java.util.*
public class MsgQueue{
private Vector queue = null;
public MsgQueue(){
queue = new Vector();
public synchronized void send(Object o)
{
queue.addElement(o);
public synchronized Object recv()
if(queue.size()==0)
Object o = queue.firstElement();
queue.removeElementAt(0);//or queue[0] = null can also work
return o;
因为java中是locked by object的所以添加synchronized 就可以用于线程同步锁定对象
可以作为多线程处理多任务的存放task的队列.他的client包括封装好的task类以及thread类
① 线程的几种状态
线程有四种状态,任何一个线程肯定处于这四种状态中的一种:
①.) 产生(New):线程对象已经产生,但尚未被启动,所以无法执行.如通过new产生了一个线程对象后没对它调用start()函数之前.
①.) 通过suspend()函数,可使线程进入停滞状态.通过suspend()使线程进入停滞状态后,除非收到resume()消息,否则该线程不会变回可执行状态.
例11:
class TestThreadMethod extends Thread{
public static int shareVar = 0;
public TestThreadMethod(String name){
super(name);
public synchronized void run(){
if(shareVar==0){
shareVar++;
this.suspend(); //(1)
}}}
else{
System.out.print(Thread.currentThread().getName());
System.out.println(" shareVar = " + shareVar);
}}
public class TestThread{
public static void main(String[] args){
TestThreadMethod t1 = new TestThreadMethod("t1");
运行结果为:
①.) sleep ()函数有一个参数,通过参数可使线程在指定的时间内进入停滞状态,当指定的时间过后,线程则自动进入可执行状态.
System.out.println(" : " + i);
try{
catch(InterruptedException e){
System.out.println("Interrupted");
public class TestThread{public static void main(String[] args){
t1.start(); (1)
t1 : 0
t1 : 1
if(Thread.currentThread().getName().equals("t1"))
else
Thread.sleep(100);
t1.start();
//t1.start();
①.) 通过yield ()函数,可使线程进入可执行状态,排程器从可执行状态的线程中重新进行排程.所以调用了yield()的函数也有可能马上被执行.
public TestThreadMethod(String name){super(name);
Thread.yield();
t1.start(); //(1)
从结果可知调用yield()时并不会释放对象的"锁标志".
从结果可知,虽然t1线程调用了yield(),但它马上又被执行了.
①.) sleep()使当前线程进入停滞状态,所以执行sleep()的线程在指定的时间内肯定不会执行;yield()只是使当前线程重新回到可执行状态,所以执行yield()的线程有可能在进入到可执行状态后马上又被执行.
public void run(){
//Thread.yield(); (1)
t1.setPriority(Thread.MAX_PRIORITY);
可见,调用yield(),不同优先级的线程永远不会得到执行机会.
使调用join()的线程执行完毕后才能执行其它线程,在一定意义上,它可以实现同步的功能.
System.out.println(" " + i);
t1.join();
catch(InterruptedException e){}
wait()、notify()和notifyAll()这三个函数由java.lang.Object类提供,用于协调多个线程对共享数据的存取.
①.) wait()函数有两种形式:第一种形式接受一个毫秒值,用于在指定时间长度内暂停线程,使线程进入停滞状态.第二种形式为不带参数,代表waite()在notify()或notifyAll()之前会持续停滞.
下面,我们将对例11中的例子进行修改
for(int i=0; i10; i++){
if(shareVar!=0){
t1 shareVar = 10
waite()和notify()因为会对对象的"锁标志"进行操作,所以它们必须在synchronized函数或synchronized block中进行调用.如果在non-synchronized函数或non-synchronized block中进行调用,虽然能编译通过,但在运行时会发生IllegalMonitorStateException的异常.
public int shareVar = 0;
new Notifier(this);
System.out.println("i = " + shareVar);
System.out.println("wait......");
this.wait();
class Notifier extends Thread{
private TestThreadMethod ttm;
Notifier(TestThreadMethod t){
ttm = t;
start();
while(true){
/*1 要同步的不是当前对象的做法 */
synchronized(ttm){
System.out.println("notify......");
ttm.notify();
i = 1
wait......
notify......
①.) wait()使当前线程进入停滞状态时,还会释放当前线程所占有的"锁标志",从而使线程对象中的synchronized资源可被对象中别的线程使用;而suspend()和sleep()使当前线程进入停滞状态时不会释放当前线程所占有的"锁标志".
class StackT {
private VectorT v;
public Stack(){
v = new VectorT();
public T pop(){
if (v.size()==0) return null;
return v.get(v.size()-1);
public void push(T t){
v.add(t);
public boolean isEmpty(){
return v.size()==0;
class QueueT{
public Queue(){
//入队列
public void enqueue(T t){
//出队列
public T dequeue(){
return v.get(0);
return v.size() == 0;
import java.util.*;
public class MyQueueT {
private LinkedListT list = new LinkedListT();
public void addLast(T v) {
list.addLast(v); //队尾插入
public T getFirst() {
return list.getFirst(); //取得队受元素
public void remove() {
list.removeFirst(); //移除队首元素
//类似功能自己扩展下
MyQueueString mq = new MyQueueString();
mq.addLast("hello world");
System.out.println(mq.getFirst());
mq.remove();
简单写了下,希望你能看明白
import java.util.ArrayList;
public class SeqQueue {
ArrayListString list;
public SeqQueue() {
list = new ArrayListString();
public String getFirst() {
if (!list.isEmpty()) {
String s = list.get(0);
list.remove(0);
return s;
public void insertLast(String s) {
list.add(s);
SeqQueue seq = new SeqQueue();
seq.insertLast("111");
System.out.println(seq.getFirst());
以上就是土嘎嘎小编为大家整理的java实现队列的代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!