网站首页 > 文章中心 > 其它

队列的java代码实现

作者:小编 更新时间:2023-10-15 13:09:48 浏览量:169人看过

怎样用java代码实现一个队列

class StackT {

private VectorT v;

public Stack(){

v = new VectorT();

}

public T pop(){

if (v.size()==0) return null;

队列的java代码实现-图1

return v.get(v.size()-1);

public void push(T t){

v.add(t);

public boolean isEmpty(){

return v.size()==0;

class QueueT{

队列的java代码实现-图2

public Queue(){

//入队列

public void enqueue(T t){

//出队列

public T dequeue(){

return v.get(0);

return v.size() == 0;

用java编一个队列

自己写了个简单的实现

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;

在JAVA中怎么实现消息队列

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)

return?null;

队列的java代码实现-图3

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()使当前线程进入停滞状态时不会释放当前线程所占有的"锁标志".

怎么编写一个简单的java队列?

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(); //移除队首元素

//类似功能自己扩展下

public static void main(String[] args) {

MyQueueString mq = new MyQueueString();

mq.addLast("hello world");

System.out.println(mq.getFirst());

mq.remove();

以上就是土嘎嘎小编为大家整理的队列的java代码实现相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章