class StackT {

為涿州等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及涿州網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計制作、做網(wǎng)站、涿州網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
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{
private VectorT v;
public Queue(){
v = new VectorT();
}
//入隊列
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;
}
}
自己寫了個簡單的實現(xiàn)
class QueueE{
private Object[] integerQueue;//用來當(dāng)隊列
public int tail;//隊尾
public int size;//隊的長度,也可以設(shè)置一個默認(rèn)值,溢出時從新申請
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;
}else{
return null;
}
}
}
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;
Object?o?=?queue.firstElement();
queue.removeElementAt(0);//or?queue[0]?=?null?can?also?work
return?o;
}
}
因為java中是locked?by?object的所以添加synchronized?就可以用于線程同步鎖定對象
可以作為多線程處理多任務(wù)的存放task的隊列。他的client包括封裝好的task類以及thread類
Java的多線程-線程間的通信2009-08-25?21:58
1.?線程的幾種狀態(tài)
線程有四種狀態(tài),任何一個線程肯定處于這四種狀態(tài)中的一種:
1)?產(chǎn)生(New):線程對象已經(jīng)產(chǎn)生,但尚未被啟動,所以無法執(zhí)行。如通過new產(chǎn)生了一個線程對象后沒對它調(diào)用start()函數(shù)之前。
2)?可執(zhí)行(Runnable):每個支持多線程的系統(tǒng)都有一個排程器,排程器會從線程池中選擇一個線程并啟動它。當(dāng)一個線程處于可執(zhí)行狀態(tài)時,表示它可能正處于線程池中等待排排程器啟動它;也可能它已正在執(zhí)行。如執(zhí)行了一個線程對象的start()方法后,線程就處于可執(zhí)行狀態(tài),但顯而易見的是此時線程不一定正在執(zhí)行中。
3)?死亡(Dead):當(dāng)一個線程正常結(jié)束,它便處于死亡狀態(tài)。如一個線程的run()函數(shù)執(zhí)行完畢后線程就進(jìn)入死亡狀態(tài)。
4)?停滯(Blocked):當(dāng)一個線程處于停滯狀態(tài)時,系統(tǒng)排程器就會忽略它,不對它進(jìn)行排程。當(dāng)處于停滯狀態(tài)的線程重新回到可執(zhí)行狀態(tài)時,它有可能重新執(zhí)行。如通過對一個線程調(diào)用wait()函數(shù)后,線程就進(jìn)入停滯狀態(tài),只有當(dāng)兩次對該線程調(diào)用notify或notifyAll后它才能兩次回到可執(zhí)行狀態(tài)。
2.?class Thread下的常用函數(shù)函數(shù)
2.1?suspend()、resume()
1)?通過suspend()函數(shù),可使線程進(jìn)入停滯狀態(tài)。通過suspend()使線程進(jìn)入停滯狀態(tài)后,除非收到resume()消息,否則該線程不會變回可執(zhí)行狀態(tài)。
2)?當(dāng)調(diào)用suspend()函數(shù)后,線程不會釋放它的“鎖標(biāo)志”。
例11:
class?TestThreadMethod?extends?Thread{
public?static?int?shareVar?=?0;
public?TestThreadMethod(String?name){
super(name);
}
public?synchronized?void?run(){
if(shareVar==0){
for(int?i=0;?i5;?i++){
shareVar++;
if(shareVar==5){
this.suspend(); //(1)
}}}
else{
System.out.print(Thread.currentThread().getName());
System.out.println("?shareVar?=?"?+?shareVar);
this.resume(); //(2)
}}
}
public?class?TestThread{
public?static?void?main(String[]?args){
TestThreadMethod?t1?=?new?TestThreadMethod("t1");
TestThreadMethod?t2?=?new?TestThreadMethod("t2");
t1.start(); //(5)
//t1.start(); //(3)
t2.start(); //(4)
}}
運行結(jié)果為:
t2?shareVar?=?5
i.?當(dāng)代碼(5)的t1所產(chǎn)生的線程運行到代碼(1)處時,該線程進(jìn)入停滯狀態(tài)。然后排程器從線程池中喚起代碼(4)的t2所產(chǎn)生的線程,此時shareVar值不為0,所以執(zhí)行else中的語句。
ii.?也許你會問,那執(zhí)行代碼(2)后為什么不會使t1進(jìn)入可執(zhí)行狀態(tài)呢?正如前面所說,t1和t2是兩個不同對象的線程,而代碼(1)和(2)都只對當(dāng)前對象進(jìn)行操作,所以t1所產(chǎn)生的線程執(zhí)行代碼(1)的結(jié)果是對象t1的當(dāng)前線程進(jìn)入停滯狀態(tài);而t2所產(chǎn)生的線程執(zhí)行代碼(2)的結(jié)果是把對象t2中的所有處于停滯狀態(tài)的線程調(diào)回到可執(zhí)行狀態(tài)。
iii.?那現(xiàn)在把代碼(4)注釋掉,并去掉代碼(3)的注釋,是不是就能使t1重新回到可執(zhí)行狀態(tài)呢?運行結(jié)果是什么也不輸出。為什么會這樣呢?也許你會認(rèn)為,當(dāng)代碼(5)所產(chǎn)生的線程執(zhí)行到代碼(1)時,它進(jìn)入停滯狀態(tài);而代碼(3)所產(chǎn)生的線程和代碼(5)所產(chǎn)生的線程是屬于同一個對象的,那么就當(dāng)代碼(3)所產(chǎn)生的線程執(zhí)行到代碼(2)時,就可使代碼(5)所產(chǎn)生的線程執(zhí)行回到可執(zhí)行狀態(tài)。但是要清楚,suspend()函數(shù)只是讓當(dāng)前線程進(jìn)入停滯狀態(tài),但并不釋放當(dāng)前線程所獲得的“鎖標(biāo)志”。所以當(dāng)代碼(5)所產(chǎn)生的線程進(jìn)入停滯狀態(tài)時,代碼(3)所產(chǎn)生的線程仍不能啟動,因為當(dāng)前對象的“鎖標(biāo)志”仍被代碼(5)所產(chǎn)生的線程占有。
#p#2.2?sleep()
1)?sleep?()函數(shù)有一個參數(shù),通過參數(shù)可使線程在指定的時間內(nèi)進(jìn)入停滯狀態(tài),當(dāng)指定的時間過后,線程則自動進(jìn)入可執(zhí)行狀態(tài)。
2)?當(dāng)調(diào)用sleep?()函數(shù)后,線程不會釋放它的“鎖標(biāo)志”。
例12:
class?TestThreadMethod?extends?Thread{
class?TestThreadMethod?extends?Thread{
public?static?int?shareVar?=?0;
public?TestThreadMethod(String?name){
super(name);
}
public?synchronized?void?run(){
for(int?i=0;?i3;?i++){
System.out.print(Thread.currentThread().getName());
System.out.println("?:?"?+?i);
try{
Thread.sleep(100); //(4)
}
catch(InterruptedException?e){
System.out.println("Interrupted");
}}}
}
public?class?TestThread{public?static?void?main(String[]?args){
TestThreadMethod?t1?=?new?TestThreadMethod("t1");
TestThreadMethod?t2?=?new?TestThreadMethod("t2");
t1.start(); (1)
t1.start(); (2)
//t2.start(); (3)
}}
運行結(jié)果為:
t1?:?0
t1?:?1
t1?:?2
t1?:?0
t1?:?1
t1?:?2
由結(jié)果可證明,雖然在run()中執(zhí)行了sleep(),但是它不會釋放對象的“鎖標(biāo)志”,所以除非代碼(1)的線程執(zhí)行完run()函數(shù)并釋放對象的“鎖標(biāo)志”,否則代碼(2)的線程永遠(yuǎn)不會執(zhí)行。
如果把代碼(2)注釋掉,并去掉代碼(3)的注釋,結(jié)果將變?yōu)椋?/p>
t1?:?0
t2?:?0
t1?:?1
t2?:?1
t1?:?2
t2?:?2
由于t1和t2是兩個對象的線程,所以當(dāng)線程t1通過sleep()進(jìn)入停滯時,排程器會從線程池中調(diào)用其它的可執(zhí)行線程,從而t2線程被啟動。
例13:
class?TestThreadMethod?extends?Thread{
public?static?int?shareVar?=?0;
public?TestThreadMethod(String?name){
super(name);
}
public?synchronized?void?run(){
for(int?i=0;?i5;?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(InterruptedException?e){
System.out.println("Interrupted");
}}
}}
public?class?TestThread{public?static?void?main(String[]?args){
TestThreadMethod?t1?=?new?TestThreadMethod("t1");
TestThreadMethod?t2?=?new?TestThreadMethod("t2");
t1.start();
//t1.start();
t2.start();
}}
運行結(jié)果為:
t1?:?0
t2?:?0
t2?:?1
t1?:?1
t2?:?2
t2?:?3
t1?:?2
t2?:?4
t1?:?3
t1?:?4
由于線程t1調(diào)用了sleep(200),而線程t2調(diào)用了sleep(100),所以線程t2處于停滯狀態(tài)的時間是線程t1的一半,從從結(jié)果反映出來的就是線程t2打印兩倍次線程t1才打印一次。
#p#2.3?yield()
1)?通過yield?()函數(shù),可使線程進(jìn)入可執(zhí)行狀態(tài),排程器從可執(zhí)行狀態(tài)的線程中重新進(jìn)行排程。所以調(diào)用了yield()的函數(shù)也有可能馬上被執(zhí)行。
2)?當(dāng)調(diào)用yield?()函數(shù)后,線程不會釋放它的“鎖標(biāo)志”。
例14:
class?TestThreadMethod?extends?Thread{
public?static?int?shareVar?=?0;
public?TestThreadMethod(String?name){super(name);
}
public?synchronized?void?run(){for(int?i=0;?i4;?i++){
System.out.print(Thread.currentThread().getName());
System.out.println("?:?"?+?i);
Thread.yield();
}}
}
public?class?TestThread{public?static?void?main(String[]?args){
TestThreadMethod?t1?=?new?TestThreadMethod("t1");
TestThreadMethod?t2?=?new?TestThreadMethod("t2");
t1.start();
t1.start(); //(1)
//t2.start(); (2)
}
}
運行結(jié)果為:
t1?:?0
t1?:?1
t1?:?2
t1?:?3
t1?:?0
t1?:?1
t1?:?2
t1?:?3
從結(jié)果可知調(diào)用yield()時并不會釋放對象的“鎖標(biāo)志”。
如果把代碼(1)注釋掉,并去掉代碼(2)的注釋,結(jié)果為:
t1?:?0
t1?:?1
t2?:?0
t1?:?2
t2?:?1
t1?:?3
t2?:?2
t2?:?3
從結(jié)果可知,雖然t1線程調(diào)用了yield(),但它馬上又被執(zhí)行了。
2.4?sleep()和yield()的區(qū)別
1)?sleep()使當(dāng)前線程進(jìn)入停滯狀態(tài),所以執(zhí)行sleep()的線程在指定的時間內(nèi)肯定不會執(zhí)行;yield()只是使當(dāng)前線程重新回到可執(zhí)行狀態(tài),所以執(zhí)行yield()的線程有可能在進(jìn)入到可執(zhí)行狀態(tài)后馬上又被執(zhí)行。
2)?sleep()可使優(yōu)先級低的線程得到執(zhí)行的機會,當(dāng)然也可以讓同優(yōu)先級和高優(yōu)先級的線程有執(zhí)行的機會;yield()只能使同優(yōu)先級的線程有執(zhí)行的機會。
例15:
class?TestThreadMethod?extends?Thread{
public?static?int?shareVar?=?0;
public?TestThreadMethod(String?name){
super(name);
}
public?void?run(){
for(int?i=0;?i4;?i++){
System.out.print(Thread.currentThread().getName());
System.out.println("?:?"?+?i);
//Thread.yield(); (1)
/*?(2)?*/
try{
Thread.sleep(3000);
}
catch(InterruptedException?e){
System.out.println("Interrupted");
}}}
}
public?class?TestThread{
public?static?void?main(String[]?args){
TestThreadMethod?t1?=?new?TestThreadMethod("t1");
TestThreadMethod?t2?=?new?TestThreadMethod("t2");
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
}
}
運行結(jié)果為:
t1?:?0
t1?:?1
t2?:?0
t1?:?2
t2?:?1
t1?:?3
t2?:?2
t2?:?3
由結(jié)果可見,通過sleep()可使優(yōu)先級較低的線程有執(zhí)行的機會。注釋掉代碼(2),并去掉代碼(1)的注釋,結(jié)果為:
t1?:?0
t1?:?1
t1?:?2
t1?:?3
t2?:?0
t2?:?1
t2?:?2
t2?:?3
可見,調(diào)用yield(),不同優(yōu)先級的線程永遠(yuǎn)不會得到執(zhí)行機會。
2.5?join()
使調(diào)用join()的線程執(zhí)行完畢后才能執(zhí)行其它線程,在一定意義上,它可以實現(xiàn)同步的功能。
例16:
class?TestThreadMethod?extends?Thread{
public?static?int?shareVar?=?0;
public?TestThreadMethod(String?name){
super(name);
}
public?void?run(){
for(int?i=0;?i4;?i++){
System.out.println("?"?+?i);
try{
Thread.sleep(3000);
}
catch(InterruptedException?e){
System.out.println("Interrupted");
}
}
}
}
public?class?TestThread{
public?static?void?main(String[]?args){
TestThreadMethod?t1?=?new?TestThreadMethod("t1");
t1.start();
try{
t1.join();
}
catch(InterruptedException?e){}
t1.start();
}
}
運行結(jié)果為:
1
2
3
1
2
3
#p#3.?class Object下常用的線程函數(shù)
wait()、notify()和notifyAll()這三個函數(shù)由java.lang.Object類提供,用于協(xié)調(diào)多個線程對共享數(shù)據(jù)的存取。
3.1?wait()、notify()和notifyAll()
1)?wait()函數(shù)有兩種形式:第一種形式接受一個毫秒值,用于在指定時間長度內(nèi)暫停線程,使線程進(jìn)入停滯狀態(tài)。第二種形式為不帶參數(shù),代表waite()在notify()或notifyAll()之前會持續(xù)停滯。
2)?當(dāng)對一個對象執(zhí)行notify()時,會從線程等待池中移走該任意一個線程,并把它放到鎖標(biāo)志等待池中;當(dāng)對一個對象執(zhí)行notifyAll()時,會從線程等待池中移走所有該對象的所有線程,并把它們放到鎖標(biāo)志等待池中。
3)?當(dāng)調(diào)用wait()后,線程會釋放掉它所占有的“鎖標(biāo)志”,從而使線程所在對象中的其它synchronized數(shù)據(jù)可被別的線程使用。
例17:
下面,我們將對例11中的例子進(jìn)行修改
class?TestThreadMethod?extends?Thread{
public?static?int?shareVar?=?0;
public?TestThreadMethod(String?name){
super(name);
}
public?synchronized?void?run(){
if(shareVar==0){
for(int?i=0;?i10;?i++){
shareVar++;
if(shareVar==5){
try{
this.wait(); //(4)
}
catch(InterruptedException?e){}
}
}
}
if(shareVar!=0){
System.out.print(Thread.currentThread().getName());
System.out.println("?shareVar?=?"?+?shareVar);
this.notify(); //(5)
}
}
}
public?class?TestThread{
public?static?void?main(String[]?args){
TestThreadMethod?t1?=?new?TestThreadMethod("t1");
TestThreadMethod?t2?=?new?TestThreadMethod("t2");
t1.start(); //(1)
//t1.start(); (2)
t2.start(); //(3)
}}
運行結(jié)果為:
t2?shareVar?=?5
因為t1和t2是兩個不同對象,所以線程t2調(diào)用代碼(5)不能喚起線程t1。如果去掉代碼(2)的注釋,并注釋掉代碼(3),結(jié)果為:
t1?shareVar?=?5
t1?shareVar?=?10
這是因為,當(dāng)代碼(1)的線程執(zhí)行到代碼(4)時,它進(jìn)入停滯狀態(tài),并釋放對象的鎖狀態(tài)。接著,代碼(2)的線程執(zhí)行run(),由于此時shareVar值為5,所以執(zhí)行打印語句并調(diào)用代碼(5)使代碼(1)的線程進(jìn)入可執(zhí)行狀態(tài),然后代碼(2)的線程結(jié)束。當(dāng)代碼(1)的線程重新執(zhí)行后,它接著執(zhí)行for()循環(huán)一直到shareVar=10,然后打印shareVar。
#p#3.2?wait()、notify()和synchronized
waite()和notify()因為會對對象的“鎖標(biāo)志”進(jìn)行操作,所以它們必須在synchronized函數(shù)或synchronized block中進(jìn)行調(diào)用。如果在non-synchronized函數(shù)或non-synchronized block中進(jìn)行調(diào)用,雖然能編譯通過,但在運行時會發(fā)生IllegalMonitorStateException的異常。
例18:
class?TestThreadMethod?extends?Thread{
public?int?shareVar?=?0;
public?TestThreadMethod(String?name){
super(name);
new?Notifier(this);
}
public?synchronized?void?run(){
if(shareVar==0){
for(int?i=0;?i5;?i++){
shareVar++;
System.out.println("i?=?"?+?shareVar);
try{
System.out.println("wait......");
this.wait();
}
catch(InterruptedException?e){}
}}
}
}
class?Notifier?extends?Thread{
private?TestThreadMethod?ttm;
Notifier(TestThreadMethod?t){
ttm?=?t;
start();
}
public?void?run(){
while(true){
try{
sleep(2000);
}
catch(InterruptedException?e){}
/*1?要同步的不是當(dāng)前對象的做法?*/
synchronized(ttm){
System.out.println("notify......");
ttm.notify();
}}
}
}
public?class?TestThread{
public?static?void?main(String[]?args){
TestThreadMethod?t1?=?new?TestThreadMethod("t1");
t1.start();
}
}
運行結(jié)果為:
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?這兩組函數(shù)的區(qū)別
1)?wait()使當(dāng)前線程進(jìn)入停滯狀態(tài)時,還會釋放當(dāng)前線程所占有的“鎖標(biāo)志”,從而使線程對象中的synchronized資源可被對象中別的線程使用;而suspend()和sleep()使當(dāng)前線程進(jìn)入停滯狀態(tài)時不會釋放當(dāng)前線程所占有的“鎖標(biāo)志”。
2)?前一組函數(shù)必須在synchronized函數(shù)或synchronized block中調(diào)用,否則在運行時會產(chǎn)生錯誤;而后一組函數(shù)可以non-synchronized函數(shù)和synchronized block中調(diào)用。
4.2?這兩組函數(shù)的取舍
Java2已不建議使用后一組函數(shù)。因為在調(diào)用suspend()時不會釋放當(dāng)前線程所取得的“鎖標(biāo)志”,這樣很容易造成“死鎖”。
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");
mq.addLast("hello world2");
System.out.println(mq.getFirst());
mq.remove();
System.out.println(mq.getFirst());
}
}
網(wǎng)站欄目:隊列的java代碼實現(xiàn),java中隊列的實現(xiàn)
文章位置:http://www.chinadenli.net/article36/heogsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、App開發(fā)、虛擬主機、商城網(wǎng)站、網(wǎng)站維護、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)