導航:首頁 > 編程語言 > 實現一個隊列java

實現一個隊列java

發布時間:2022-05-18 17:52:28

A. java用數組實現隊列

1.1. 隊列的數據結構

隊列是一種特殊的線性表,特殊之處在於它只允許在表的前端(front)進行刪除操作,而在表的後端(rear)進行插入操作,和棧一樣,隊列是一種操作受限制的線性表。進行插入操作的端稱為隊尾,進行刪除操作的端稱為隊頭。

1.2. Java實現

QueueTest

package ch04;

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

/**
* 判斷隊列是否已滿
*
* @return
*/
public boolean isFull() {
return arrInt.length - 1 == rear;
}

/**
* 向隊列的隊尾插入一個元素
*/
public void insert(int item) {
if (isFull()) {
throw new RuntimeException("隊列已滿");
}
arrInt[++rear] = item;
}

/**
* 獲得對頭元素
*
* @return
*/
public int peekFront() {
return arrInt[front];
}

/**
* 獲得隊尾元素
*
* @return
*/
public int peekRear() {
return arrInt[rear];
}

/**
* 從隊列的對頭移除一個元素
*
* @return
*/
public int remove() {
if (isEmpty()) {
throw new RuntimeException("隊列為空");
}
return arrInt[front++];
}
}

運行結果如下:

false

true

0

1

2

3

4

5

6

7

8

9

B. 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;
}
}

C. java多線程共同操作同一個隊列,怎麼實現

以下是兩個線程:

import java.util.*;

public class Thread_List_Operation {
//假設有這么一個隊列
static List list = new LinkedList();

public static void main(String[] args) {
Thread t;
t = new Thread(new T1());
t.start();
t = new Thread(new T2());
t.start();

}

}

//線程T1,用來給list添加新元素
class T1 implements Runnable{

void getElemt(Object o){
Thread_List_Operation.list.add(o);
System.out.println(Thread.currentThread().getName() + "為隊列添加了一個元素");
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
getElemt(new Integer(1));
}

}

}

//線程T2,用來給list添加新元素
class T2 implements Runnable{

void getElemt(Object o){
Thread_List_Operation.list.add(o);
System.out.println(Thread.currentThread().getName() + "為隊列添加了一個元素");
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
getElemt(new Integer(1));
}

}

}

//結果(亂序)
Thread-0為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素

D. java 用什麼實現 FIFO隊列

java使用數據結構來實現FIFO先進先出的隊列,實例如下:

/*
*Tochangethistemplate,chooseTools|Templates
*andopenthetemplateintheeditor.
*/
packagelinkedlisttest;

importjava.util.ArrayList;
importjava.util.Deque;
importjava.util.LinkedList;
importjava.util.List;

/**
*
*@authorVicky.H
*@[email protected]
*/
publicclassFIFOTest{

/**
*@
*/
publicstaticvoidmain(String[]args){
FIFO<A>fifo=newFIFOImpl<A>(5);
for(inti=0;i<20;i++){
Aa=newA("A:"+i);
Ahead=fifo.addLastSafe(a);
System.out.println(i+" head:"+head+" size:"+fifo.size());
}

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

System.out.println("彈出數據");
List<A>polls=fifo.setMaxSize(3);
for(Aa:polls){
System.out.println(" head:"+a);
}

System.out.println("剩餘數據");
for(Aa:fifo){
System.out.println(" head:"+a);
}
System.out.println(fifo.size());
}
}

interfaceFIFO<T>extendsList<T>,Deque<T>,Cloneable,java.io.Serializable{

/**
*向最後添加一個新的,如果長度超過允許的最大值,則彈出一個*
*/
TaddLastSafe(TaddLast);

/**
*彈出head,如果Size=0返回null。而不同於pop拋出異常
*@return
*/
TpollSafe();

/**
*獲得最大保存
*
*@return
*/
intgetMaxSize();

/**
*設置最大存儲范圍
*
*@return返回的是,因為改變了隊列大小,導致彈出的head
*/
List<T>setMaxSize(intmaxSize);

}

classFIFOImpl<T>extendsLinkedList<T>implementsFIFO<T>{

privateintmaxSize=Integer.MAX_VALUE;
privatefinalObjectsynObj=newObject();

publicFIFOImpl(){
super();
}

publicFIFOImpl(intmaxSize){
super();
this.maxSize=maxSize;
}

@Override
publicTaddLastSafe(TaddLast){
synchronized(synObj){
Thead=null;
while(size()>=maxSize){
head=poll();
}
addLast(addLast);
returnhead;
}
}

@Override
publicTpollSafe(){
synchronized(synObj){
returnpoll();
}
}

@Override
publicList<T>setMaxSize(intmaxSize){
List<T>list=null;
if(maxSize<this.maxSize){
list=newArrayList<T>();
synchronized(synObj){
while(size()>maxSize){
list.add(poll());
}
}
}
this.maxSize=maxSize;
returnlist;
}

@Override
publicintgetMaxSize(){
returnthis.maxSize;
}
}

classA{

privateStringname;

publicA(){
}

publicA(Stringname){
this.name=name;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

@Override
publicStringtoString(){
return"A{"+"name="+name+'}';
}
}

E. 到底什麼是消息隊列Java中如何實現消息隊列

「消息隊列」是在消息的傳輸過程中保存消息的容器。和我們學過的LinkedHashMap,TreeSet等一樣,都是容器。既然是容器,就有有自己的特性,就像LinkedHashMap是以鍵值對存儲。存取順序不變。而消息隊列,看到隊列就可以知道。這個容器裡面的消息是站好隊的,一般遵從先進先出原則。

java中已經為我們封裝好了很多的消息隊列。在java 1.5版本時推出的java.util.concurrent中有很多現成的隊列供我們使用。特性繁多,種類齊全。是你居家旅遊開發必備QAQ。

下面簡單列舉這個包中的消息隊列

  1. :阻塞隊列 BlockingQueue

  2. 數組阻塞隊列 ArrayBlockingQueue

  3. 延遲隊列 DelayQueue

  4. 鏈阻塞隊列 LinkedBlockingQueue

  5. 具有優先順序的阻塞隊列 PriorityBlockingQueue

  6. 同步隊列 SynchronousQueue

  7. 阻塞雙端隊列 BlockingDeque

  8. 鏈阻塞雙端隊列 LinkedBlockingDeque

    不同的隊列不同的特性決定了隊列使用的時機,感興趣的話你可以詳細了解。具體的使用方式我就不贅述了

F. 怎樣用java代碼實現一個隊列

class Stack<T> {
private Vector<T> v;
public Stack(){
v = new Vector<T>();
}

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 Queue<T>{
private Vector<T> v;
public Queue(){
v = new Vector<T>();
}
//入隊列
public void enqueue(T t){
v.add(t);
}

//出隊列
public T dequeue(){
if (v.size()==0) return null;
return v.get(0);
}

public boolean isEmpty(){
return v.size() == 0;
}
}

G. java中創建隊列Queue的問題

因為queue是介面,不能new
介面,應該new介面實現類,你看jdk文檔,搜索queue,如圖:

看見下面有一大堆實現queue的類,選一個就行,針對隊列的,你可以選LinkedBlockingQueue,AbstrctQueue,ArrayDeque

H. 在JAVA中怎麼實現消息隊列

java中的消息隊列
消息隊列是線程間通訊的手段:

importjava.util.*

publicclassMsgQueue{

privateVectorqueue=null;
publicMsgQueue(){
queue=newVector();
}
publicsynchronizedvoidsend(Objecto)
{
queue.addElement(o);
}
publicsynchronizedObjectrecv()
{
if(queue.size()==0)
returnnull;
Objecto=queue.firstElement();
queue.removeElementAt(0);//orqueue[0]=nullcanalsowork
returno;
}
}

因為java中是lockedbyobject的所以添加synchronized就可以用於線程同步鎖定對象
可以作為多線程處理多任務的存放task的隊列。他的client包括封裝好的task類以及thread類

Java的多線程-線程間的通信2009-08-2521:58
1.線程的幾種狀態
線程有四種狀態,任何一個線程肯定處於這四種狀態中的一種:
1)產生(New):線程對象已經產生,但尚未被啟動,所以無法執行。如通過new產生了一個線程對象後沒對它調用start()函數之前。
2)可執行(Runnable):每個支持多線程的系統都有一個排程器,排程器會從線程池中選擇一個線程並啟動它。當一個線程處於可執行狀態時,表示它可能正處於線程池中等待排排程器啟動它;也可能它已正在執行。如執行了一個線程對象的start()方法後,線程就處於可執行狀態,但顯而易見的是此時線程不一定正在執行中。
3)死亡(Dead):當一個線程正常結束,它便處於死亡狀態。如一個線程的run()函數執行完畢後線程就進入死亡狀態。
4)停滯(Blocked):當一個線程處於停滯狀態時,系統排程器就會忽略它,不對它進行排程。當處於停滯狀態的線程重新回到可執行狀態時,它有可能重新執行。如通過對一個線程調用wait()函數後,線程就進入停滯狀態,只有當兩次對該線程調用notify或notifyAll後它才能兩次回到可執行狀態。
2.classThread下的常用函數函數
2.1suspend()、resume()
1)通過suspend()函數,可使線程進入停滯狀態。通過suspend()使線程進入停滯狀態後,除非收到resume()消息,否則該線程不會變回可執行狀態。
2)當調用suspend()函數後,線程不會釋放它的「鎖標志」。
例11:
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
}
publicsynchronizedvoidrun(){
if(shareVar==0){
for(inti=0;i<5;i++){
shareVar++;
if(shareVar==5){
this.suspend();//(1)
}}}
else{
System.out.print(Thread.currentThread().getName());
System.out.println("shareVar="+shareVar);
this.resume();//(2)
}}
}
publicclassTestThread{
publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
TestThreadMethodt2=newTestThreadMethod("t2");
t1.start();//(5)
//t1.start();//(3)
t2.start();//(4)
}}
運行結果為:
t2shareVar=5
i.當代碼(5)的t1所產生的線程運行到代碼(1)處時,該線程進入停滯狀態。然後排程器從線程池中喚起代碼(4)的t2所產生的線程,此時shareVar值不為0,所以執行else中的語句。
ii.也許你會問,那執行代碼(2)後為什麼不會使t1進入可執行狀態呢?正如前面所說,t1和t2是兩個不同對象的線程,而代碼(1)和(2)都只對當前對象進行操作,所以t1所產生的線程執行代碼(1)的結果是對象t1的當前線程進入停滯狀態;而t2所產生的線程執行代碼(2)的結果是把對象t2中的所有處於停滯狀態的線程調回到可執行狀態。
iii.那現在把代碼(4)注釋掉,並去掉代碼(3)的注釋,是不是就能使t1重新回到可執行狀態呢?運行結果是什麼也不輸出。為什麼會這樣呢?也許你會認為,當代碼(5)所產生的線程執行到代碼(1)時,它進入停滯狀態;而代碼(3)所產生的線程和代碼(5)所產生的線程是屬於同一個對象的,那麼就當代碼(3)所產生的線程執行到代碼(2)時,就可使代碼(5)所產生的線程執行回到可執行狀態。但是要清楚,suspend()函數只是讓當前線程進入停滯狀態,但並不釋放當前線程所獲得的「鎖標志」。所以當代碼(5)所產生的線程進入停滯狀態時,代碼(3)所產生的線程仍不能啟動,因為當前對象的「鎖標志」仍被代碼(5)所產生的線程佔有。
#p#2.2sleep()
1)sleep()函數有一個參數,通過參數可使線程在指定的時間內進入停滯狀態,當指定的時間過後,線程則自動進入可執行狀態。
2)當調用sleep()函數後,線程不會釋放它的「鎖標志」。
例12:
{
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
}
publicsynchronizedvoidrun(){
for(inti=0;i<3;i++){
System.out.print(Thread.currentThread().getName());
System.out.println(":"+i);
try{
Thread.sleep(100);//(4)
}
catch(InterruptedExceptione){
System.out.println("Interrupted");
}}}
}
publicclassTestThread{publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
TestThreadMethodt2=newTestThreadMethod("t2");
t1.start();(1)
t1.start();(2)
//t2.start();(3)
}}
運行結果為:
t1:0
t1:1
t1:2
t1:0
t1:1
t1:2
由結果可證明,雖然在run()中執行了sleep(),但是它不會釋放對象的「鎖標志」,所以除非代碼(1)的線程執行完run()函數並釋放對象的「鎖標志」,否則代碼(2)的線程永遠不會執行。
如果把代碼(2)注釋掉,並去掉代碼(3)的注釋,結果將變為:
t1:0
t2:0
t1:1
t2:1
t1:2
t2:2
由於t1和t2是兩個對象的線程,所以當線程t1通過sleep()進入停滯時,排程器會從線程池中調用其它的可執行線程,從而t2線程被啟動。
例13:
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
}
publicsynchronizedvoidrun(){
for(inti=0;i<5;i++){
System.out.print(Thread.currentThread().getName());
System.out.println(":"+i);
try{
if(Thread.currentThread().getName().equals("t1"))
Thread.sleep(200);
else
Thread.sleep(100);
}
catch(InterruptedExceptione){
System.out.println("Interrupted");
}}
}}
publicclassTestThread{publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
TestThreadMethodt2=newTestThreadMethod("t2");
t1.start();
//t1.start();
t2.start();
}}
運行結果為:
t1:0
t2:0
t2:1
t1:1
t2:2
t2:3
t1:2
t2:4
t1:3
t1:4
由於線程t1調用了sleep(200),而線程t2調用了sleep(100),所以線程t2處於停滯狀態的時間是線程t1的一半,從從結果反映出來的就是線程t2列印兩倍次線程t1才列印一次。
#p#2.3yield()
1)通過yield()函數,可使線程進入可執行狀態,排程器從可執行狀態的線程中重新進行排程。所以調用了yield()的函數也有可能馬上被執行。
2)當調用yield()函數後,線程不會釋放它的「鎖標志」。
例14:
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){super(name);
}
publicsynchronizedvoidrun(){for(inti=0;i<4;i++){
System.out.print(Thread.currentThread().getName());
System.out.println(":"+i);
Thread.yield();
}}
}
publicclassTestThread{publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
TestThreadMethodt2=newTestThreadMethod("t2");
t1.start();
t1.start();//(1)
//t2.start();(2)
}
}
運行結果為:
t1:0
t1:1
t1:2
t1:3
t1:0
t1:1
t1:2
t1:3
從結果可知調用yield()時並不會釋放對象的「鎖標志」。
如果把代碼(1)注釋掉,並去掉代碼(2)的注釋,結果為:
t1:0
t1:1
t2:0
t1:2
t2:1
t1:3
t2:2
t2:3
從結果可知,雖然t1線程調用了yield(),但它馬上又被執行了。
2.4sleep()和yield()的區別
1)sleep()使當前線程進入停滯狀態,所以執行sleep()的線程在指定的時間內肯定不會執行;yield()只是使當前線程重新回到可執行狀態,所以執行yield()的線程有可能在進入到可執行狀態後馬上又被執行。
2)sleep()可使優先順序低的線程得到執行的機會,當然也可以讓同優先順序和高優先順序的線程有執行的機會;yield()只能使同優先順序的線程有執行的機會。
例15:
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
}
publicvoidrun(){
for(inti=0;i<4;i++){
System.out.print(Thread.currentThread().getName());
System.out.println(":"+i);
//Thread.yield();(1)
/*(2)*/
try{
Thread.sleep(3000);
}
catch(InterruptedExceptione){
System.out.println("Interrupted");
}}}
}
publicclassTestThread{
publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
TestThreadMethodt2=newTestThreadMethod("t2");
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
}
}
運行結果為:
t1:0
t1:1
t2:0
t1:2
t2:1
t1:3
t2:2
t2:3
由結果可見,通過sleep()可使優先順序較低的線程有執行的機會。注釋掉代碼(2),並去掉代碼(1)的注釋,結果為:
t1:0
t1:1
t1:2
t1:3
t2:0
t2:1
t2:2
t2:3
可見,調用yield(),不同優先順序的線程永遠不會得到執行機會。
2.5join()
使調用join()的線程執行完畢後才能執行其它線程,在一定意義上,它可以實現同步的功能。
例16:
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
}
publicvoidrun(){
for(inti=0;i<4;i++){
System.out.println(""+i);
try{
Thread.sleep(3000);
}
catch(InterruptedExceptione){
System.out.println("Interrupted");
}
}
}
}
publicclassTestThread{
publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
t1.start();
try{
t1.join();
}
catch(InterruptedExceptione){}
t1.start();
}
}
運行結果為:
0
1
2
3
0
1
2
3
#p#3.classObject下常用的線程函數
wait()、notify()和notifyAll()這三個函數由java.lang.Object類提供,用於協調多個線程對共享數據的存取。
3.1wait()、notify()和notifyAll()
1)wait()函數有兩種形式:第一種形式接受一個毫秒值,用於在指定時間長度內暫停線程,使線程進入停滯狀態。第二種形式為不帶參數,代表waite()在notify()或notifyAll()之前會持續停滯。
2)當對一個對象執行notify()時,會從線程等待池中移走該任意一個線程,並把它放到鎖標志等待池中;當對一個對象執行notifyAll()時,會從線程等待池中移走所有該對象的所有線程,並把它們放到鎖標志等待池中。
3)當調用wait()後,線程會釋放掉它所佔有的「鎖標志」,從而使線程所在對象中的其它synchronized數據可被別的線程使用。
例17:
下面,我們將對例11中的例子進行修改
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
}
publicsynchronizedvoidrun(){
if(shareVar==0){
for(inti=0;i<10;i++){
shareVar++;
if(shareVar==5){
try{
this.wait();//(4)
}
catch(InterruptedExceptione){}
}
}
}
if(shareVar!=0){
System.out.print(Thread.currentThread().getName());
System.out.println("shareVar="+shareVar);
this.notify();//(5)
}
}
}
publicclassTestThread{
publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
TestThreadMethodt2=newTestThreadMethod("t2");
t1.start();//(1)
//t1.start();(2)
t2.start();//(3)
}}
運行結果為:
t2shareVar=5
因為t1和t2是兩個不同對象,所以線程t2調用代碼(5)不能喚起線程t1。如果去掉代碼(2)的注釋,並注釋掉代碼(3),結果為:
t1shareVar=5
t1shareVar=10
這是因為,當代碼(1)的線程執行到代碼(4)時,它進入停滯狀態,並釋放對象的鎖狀態。接著,代碼(2)的線程執行run(),由於此時shareVar值為5,所以執行列印語句並調用代碼(5)使代碼(1)的線程進入可執行狀態,然後代碼(2)的線程結束。當代碼(1)的線程重新執行後,它接著執行for()循環一直到shareVar=10,然後列印shareVar。
#p#3.2wait()、notify()和synchronized
waite()和notify()因為會對對象的「鎖標志」進行操作,所以它們必須在synchronized函數或synchronizedblock中進行調用。如果在non-synchronized函數或non-synchronizedblock中進行調用,雖然能編譯通過,但在運行時會發生IllegalMonitorStateException的異常。
例18:
{
publicintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
newNotifier(this);
}
publicsynchronizedvoidrun(){
if(shareVar==0){
for(inti=0;i<5;i++){
shareVar++;
System.out.println("i="+shareVar);
try{
System.out.println("wait......");
this.wait();
}
catch(InterruptedExceptione){}
}}
}
}
classNotifierextendsThread{
privateTestThreadMethodttm;
Notifier(TestThreadMethodt){
ttm=t;
start();
}
publicvoidrun(){
while(true){
try{
sleep(2000);
}
catch(InterruptedExceptione){}
/*1要同步的不是當前對象的做法*/
synchronized(ttm){
System.out.println("notify......");
ttm.notify();
}}
}
}
publicclassTestThread{
publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
t1.start();
}
}
運行結果為:
i=1
wait......
notify......
i=2
wait......
notify......
i=3
wait......
notify......
i=4
wait......
notify......
i=5
wait......
notify......
4.wait()、notify()、notifyAll()和suspend()、resume()、sleep()的討論
4.1這兩組函數的區別
1)wait()使當前線程進入停滯狀態時,還會釋放當前線程所佔有的「鎖標志」,從而使線程對象中的synchronized資源可被對象中別的線程使用;而suspend()和sleep()使當前線程進入停滯狀態時不會釋放當前線程所佔有的「鎖標志」。
2)前一組函數必須在synchronized函數或synchronizedblock中調用,否則在運行時會產生錯誤;而後一組函數可以non-synchronized函數和synchronizedblock中調用。
4.2這兩組函數的取捨
Java2已不建議使用後一組函數。因為在調用suspend()時不會釋放當前線程所取得的「鎖標志」,這樣很容易造成「死鎖」。

I. 用兩個棧實現一個隊列,且用JAVA程序語言編寫,求思路和結果

隊列
是先進先出~
棧是先進後出
比如
stack1

stack2
來實現queue
對於queue來說
進入的數據
順序
比如是
1,2,3,4,5
出來順序也是
1,2,3,4,5
用stack實現的話可以
數據進去的時候用stack1來存
存完後出來的順序是
5,4,3,2,1
這跟queue的順序不一樣,所以需要stack2
,將stack1的數據一個個輸出存到stack2中,這樣stack2中的數據也就是1,2,3,4,5了,跟queue一樣
過程中要注意
你現在
數據時用stack1來存還是stack2來存。

J. Java使用LinkedList來模擬一個隊列(先進先出的特性)

importjava.util.LinkedList;

publicclassDemo01{
privateLinkedList<Object>linkedList;

publicDemo01(){
linkedList=newLinkedList<Object>();
}

publicvoidput(Objectobject){
linkedList.add(object);
}

publicObjectget(){
Objectobject=null;
if(linkedList.size()!=0){
object=linkedList.get(0);
linkedList.remove(0);
}
returnobject;
}

publicbooleanisEmpty(){
if(linkedList.size()!=0){
returntrue;
}else{
returnfalse;
}
}

publicstaticvoidmain(String[]args){
Demo01demo01=newDemo01();
demo01.put("1");
demo01.put("2");
System.out.println(demo01.get());
System.out.println(demo01.get());
System.out.println(demo01.isEmpty());
}
}

結果:

1

2

false

閱讀全文

與實現一個隊列java相關的資料

熱點內容
lzma源碼使用 瀏覽:748
ibm伺服器湖南經銷商雲伺服器 瀏覽:991
正規模板建站配雲伺服器商家 瀏覽:871
安卓清楚緩存命令 瀏覽:378
汽車壓縮機電磁離合器損壞怎麼修 瀏覽:507
怎麼提取安卓軟體 瀏覽:595
單片機和主機高速傳文件 瀏覽:478
男生直發加密需要剃光頭嗎 瀏覽:825
qtdesignerlinux 瀏覽:431
命令的幾要素 瀏覽:932
代理伺服器地址怎麼知道 瀏覽:172
漢語命令形 瀏覽:193
ACG官網下載的游戲怎麼解壓 瀏覽:963
stata交叉項命令 瀏覽:470
程序員老公燙頭 瀏覽:692
伺服器文件地址格式 瀏覽:131
securecrtandroid 瀏覽:176
短字元串壓縮 瀏覽:863
u盤插入後顯示加密格式化 瀏覽:944
我的世界怎麼用命令方塊獲得超級武器 瀏覽:382