wait()、notify()、notifyAll()是三個定義在Object類裏的方法,能夠用來控制線程的狀態。java
這三個方法最終調用的都是jvm級的native方法。隨着jvm運行平臺的不一樣可能有些許差別。數組
其中wait方法有三個over load方法:多線程
wait()app
wait(long)jvm
wait(long,int)spa
wait方法經過參數能夠指定等待的時長。若是沒有指定參數,默認一直等待直到被通知。線程
如下是一個演示代碼,以最簡潔的方式說明覆雜的問題:code
簡要說明下:對象
NotifyThread是用來模擬3秒鐘後通知其餘等待狀態的線程的線程類;blog
WaitThread是用來模擬等待的線程類;
等待的中間對象是flag,一個String對象;
main方法中同時啓動一個Notify線程和三個wait線程;
1 public class NotifyTest { 2 private String flag = "true"; 3 4 class NotifyThread extends Thread{ 5 public NotifyThread(String name) { 6 super(name); 7 } 8 public void run() { 9 try { 10 sleep(3000);//推遲3秒鐘通知 11 } catch (InterruptedException e) { 12 e.printStackTrace(); 13 } 14 15 flag = "false"; 16 flag.notify(); 17 } 18 }; 19 20 class WaitThread extends Thread { 21 public WaitThread(String name) { 22 super(name); 23 } 24 25 public void run() { 26 27 while (flag!="false") { 28 System.out.println(getName() + " begin waiting!"); 29 long waitTime = System.currentTimeMillis(); 30 try { 31 flag.wait(); 32 } catch (InterruptedException e) { 33 e.printStackTrace(); 34 } 35 waitTime = System.currentTimeMillis() - waitTime; 36 System.out.println("wait time :"+waitTime); 37 } 38 System.out.println(getName() + " end waiting!"); 39 40 } 41 } 42 43 public static void main(String[] args) throws InterruptedException { 44 System.out.println("Main Thread Run!"); 45 NotifyTest test = new NotifyTest(); 46 NotifyThread notifyThread =test.new NotifyThread("notify01"); 47 WaitThread waitThread01 = test.new WaitThread("waiter01"); 48 WaitThread waitThread02 = test.new WaitThread("waiter02"); 49 WaitThread waitThread03 = test.new WaitThread("waiter03"); 50 notifyThread.start(); 51 waitThread01.start(); 52 waitThread02.start(); 53 waitThread03.start(); 54 } 55 56 }
OK,若是你拿這段程序去運行下的話, 會發現根本運行不了,what happened?滿屏的java.lang.IllegalMonitorStateException。
沒錯,這段程序有不少問題,咱們一個個來看。
首先,這兒要很是注意的幾個事實是
基於以上幾點事實,咱們須要確保讓線程擁有對象的控制權。
也就是說在waitThread中執行wait方法時,要保證waitThread對flag有控制權;
在notifyThread中執行notify方法時,要保證notifyThread對flag有控制權。
線程取得控制權的方法有三:
synchronized (flag) { flag = "false"; flag.notify(); }
1 synchronized (flag) { 2 while (flag!="false") { 3 System.out.println(getName() + " begin waiting!"); 4 long waitTime = System.currentTimeMillis(); 5 try { 6 flag.wait(); 7 } catch (InterruptedException e) { 8 e.printStackTrace(); 9 } 10 waitTime = System.currentTimeMillis() - waitTime; 11 System.out.println("wait time :"+waitTime); 12 } 13 System.out.println(getName() + " end waiting!"); 14 }
咱們向前進了一步。
問題解決了嗎?
好像運行仍是報錯java.lang.IllegalMonitorStateException。what happened?
這時的異常是因爲在針對flag對象同步塊中,更改了flag對象的狀態所致使的。以下:
flag="false";
flag.notify();
對在同步塊中對flag進行了賦值操做,使得flag引用的對象改變,這時候再調用notify方法時,由於沒有控制權因此拋出異常。
咱們能夠改進一下,將flag改爲一個JavaBean,而後更改它的屬性不會影響到flag的引用。
咱們這裏改爲數組來試試,也能夠達到一樣的效果:
1 private String flag[] = {"true"};
1 synchronized (flag) { 2 flag[0] = "false"; 3 flag.notify(); 4 }
1 synchronized (flag) { 2 while (flag[0]!="false") { 3 System.out.println(getName() + " begin waiting!"); 4 long waitTime = System.currentTimeMillis(); 5 try { 6 flag.wait(); 7 8 } catch (InterruptedException e) { 9 e.printStackTrace(); 10 }
這時候再運行,再也不報異常,可是線程沒有結束是吧,沒錯,還有線程堵塞,處於wait狀態。
緣由很簡單,咱們有三個wait線程,只有一個notify線程,notify線程運行notify方法的時候,是隨機通知一個正在等待的線程,因此,如今應該還有兩個線程在waiting。
咱們只須要將NotifyThread線程類中的flag.notify()方法改爲notifyAll()就能夠了。notifyAll方法會通知全部正在等待對象控制權的線程。
最終完成版以下:
1 public class NotifyTest { 2 private String flag[] = { "true" }; 3 4 class NotifyThread extends Thread { 5 public NotifyThread(String name) { 6 super(name); 7 } 8 9 public void run() { 10 try { 11 sleep(3000); 12 } catch (InterruptedException e) { 13 e.printStackTrace(); 14 } 15 synchronized (flag) { 16 flag[0] = "false"; 17 flag.notifyAll(); 18 } 19 } 20 }; 21 22 class WaitThread extends Thread { 23 public WaitThread(String name) { 24 super(name); 25 } 26 27 public void run() { 28 synchronized (flag) { 29 while (flag[0] != "false") { 30 System.out.println(getName() + " begin waiting!"); 31 long waitTime = System.currentTimeMillis(); 32 try { 33 flag.wait(); 34 35 } catch (InterruptedException e) { 36 e.printStackTrace(); 37 } 38 waitTime = System.currentTimeMillis() - waitTime; 39 System.out.println("wait time :" + waitTime); 40 } 41 System.out.println(getName() + " end waiting!"); 42 } 43 } 44 } 45 46 public static void main(String[] args) throws InterruptedException { 47 System.out.println("Main Thread Run!"); 48 NotifyTest test = new NotifyTest(); 49 NotifyThread notifyThread = test.new NotifyThread("notify01"); 50 WaitThread waitThread01 = test.new WaitThread("waiter01"); 51 WaitThread waitThread02 = test.new WaitThread("waiter02"); 52 WaitThread waitThread03 = test.new WaitThread("waiter03"); 53 notifyThread.start(); 54 waitThread01.start(); 55 waitThread02.start(); 56 waitThread03.start(); 57 } 58 59 }