程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 徹底明白Java的多線程-線程間的通信

徹底明白Java的多線程-線程間的通信

編輯:關於JAVA

線程間的通信

1. 線程的幾種狀態

線程有四種狀態,任何一個線程肯定處於這四種狀態中的一種:

1) 產生(New):線程對象已經產生,但尚未被啟動,所以無法執行。如通過new產生了一個線程對象後沒對它調用start()函數之前。

2) 可執行(Runnable):每個支持多線程的系統都有一個排程器,排程器會從線程池中選擇一個線程並啟動它。當一個線程處於可執行狀態時,表示它可能正處於線程池中等待排排程器啟動它;也可能它已正在執行。如執行了一個線程對象的start()方法後,線程就處於可執行狀態,但顯而易見的是此時線程不一定正在執行中。

3) 死亡(Dead):當一個線程正常結束,它便處於死亡狀態。如一個線程的run()函數執行完畢後線程就進入死亡狀態。

4) 停滯(Blocked):當一個線程處於停滯狀態時,系統排程器就會忽略它,不對它進行排程。當處於停滯狀態的線程重新回到可執行狀態時,它有可能重新執行。如通過對一個線程調用wait()函數後,線程就進入停滯狀態,只有當兩次對該線程調用notify或notifyAll後它才能兩次回到可執行狀態。

2. class Thread下的常用函數函數

2.1 suspend()、resume()

1) 通過suspend()函數,可使線程進入停滯狀態。通過suspend()使線程進入停滯狀態後,除非收到resume()消息,否則該線程不會變回可執行狀態。

2) 當調用suspend()函數後,線程不會釋放它的“鎖標志”。

例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; 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)

}

}

}

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)

}

}

運行結果為:

t2 shareVar = 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)所產生的線程占有。

2.2 sleep()

1) sleep ()函數有一個參數,通過參數可使線程在指定的時間內進入停滯狀態,當指定的時間過後,線程則自動進入可執行狀態。

2) 當調用sleep ()函數後,線程不會釋放它的“鎖標志”。

例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; i<3; 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)

}

}

運行結果為:

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:

   class TestThreadMethod extends Thread{

   public static int shareVar = 0;

   public TestThreadMethod(String name){

   super(name);

   }

   public synchronized void run(){

   for(int i=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(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();

   }

   }

   運行結果為:

   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才打印一次。

   2.3 yield()

   1) 通過yield ()函數,可使線程進入可執行狀態,排程器從可執行狀態的線程中重新進行排程。所以調用了yield()的函數也有可能馬上被執行。

   2) 當調用yield ()函數後,線程不會釋放它的“鎖標志”。

   例14:

   class TestThreadMethod extends Thread{

   public static int shareVar = 0;

   public TestThreadMethod(String name){

   super(name);

   }

   public synchronized void run(){

   for(int i=0; i<4; 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)

   }

   }

   運行結果為:

   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.4 sleep()和yield()的區別

   1) sleep()使當前線程進入停滯狀態,所以執行sleep()的線程在指定的時間內肯定不會執行;yield()只是使當前線程重新回到可執行狀態,所以執行yield()的線程有可能在進入到可執行狀態後馬上又被執行。

   2) sleep()可使優先級低的線程得到執行的機會,當然也可以讓同優先級和高優先級的線程有執行的機會;yield()只能使同優先級的線程有執行的機會。

   例15:

   class TestThreadMethod extends Thread{

   public static int shareVar = 0;

   public TestThreadMethod(String name){

   super(name);

   }

   public void run(){

   for(int i=0; i<4; 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();

   }

   }

   運行結果為:

   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(),不同優先級的線程永遠不會得到執行機會。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved