輪詢
線程本身是操作系統中獨立的個體,但是線程與線程之間不是獨立的個體,因為它們彼此之間要相互通信和協作。
想像一個場景,A線程做int型變量i的累加操作,B線程等待i到了10000就打印出i,怎么處理?一個辦法就是,B線程while(i == 10000),這樣兩個線程之間就有了通信,B線程不斷通過輪訓來檢測i == 10000這個條件。
這樣可以實現我們的需求,但是也帶來了問題:CPU把資源浪費了B線程的輪詢操作上,因為while操作並不釋放CPU資源,導致了CPU會一直在這個線程中做判斷操作。如果可以把這些輪詢的時間釋放出來,給別的線程用,就好了。
wait/notify
在Object對象中有三個方法wait()、notify()、notifyAll(),既然是Object中的方法,那每個對象自然都是有的。如果不接觸多線程的話,這兩個方法是不太常見的。下面看一下前兩個方法:
1、wait()
wait()的作用是使當前執行代碼的線程進行等待,將當前線程置入"預執行隊列"中,並且wait()所在的代碼處停止執行,直到接到通知或被中斷。在調用wait()之前,線程必須獲得該對象的鎖,因此只能在同步方法/同步代碼塊中調用wait()方法。
2、notify()
notify()的作用是,如果有多個線程等待,那么線程規划器隨機挑選出一個wait的線程,對其發出通知notify(),並使它等待獲取該對象的對象鎖。注意"等待獲取該對象的對象鎖",這意味着,即使收到了通知,wait的線程也不會馬上獲取對象鎖,必須等待notify()方法的線程釋放鎖才可以。和wait()一樣,notify()也要在同步方法/同步代碼塊中調用。
總結起來就是,wait()使線程停止運行,notify()使停止運行的線程繼續運行。
wait()/notify()使用示例
看一段代碼:
public class MyThread30_0 extends Thread { private Object lock; public MyThread30_0(Object lock) { this.lock = lock; } public void run() { try { synchronized (lock) { System.out.println("開始------wait time = " + System.currentTimeMillis()); lock.wait(); System.out.println("結束------wait time = " + System.currentTimeMillis()); } } catch (InterruptedException e) { e.printStackTrace(); } } }
public class MyThread30_1 extends Thread { private Object lock; public MyThread30_1(Object lock) { this.lock = lock; } public void run() { synchronized (lock) { System.out.println("開始------notify time = " + System.currentTimeMillis()); lock.notify(); System.out.println("結束------notify time = " + System.currentTimeMillis()); } } }
寫個main函數,同樣的Thread.sleep(3000)也是為了保證mt0先運行,這樣才能看到wait()和notify()的效果:
public static void main(String[] args) throws Exception { Object lock = new Object(); MyThread30_0 mt0 = new MyThread30_0(lock); mt0.start(); Thread.sleep(3000); MyThread30_1 mt1 = new MyThread30_1(lock); mt1.start(); }
看一下運行結果:
開始------wait time = 1443931599021 開始------notify time = 1443931602024 結束------notify time = 1443931602024 結束------wait time = 1443931602024
第一行和第二行之間的time減一下很明顯就是3s,說明wait()之后代碼一直暫停,notify()之后代碼才開始運行。
wait()方法可以使調用該線程的方法釋放共享資源的鎖,然后從運行狀態退出,進入等待隊列,直到再次被喚醒。
notify()方法可以隨機喚醒等待隊列中等待同一共享資源的一個線程,並使得該線程退出等待狀態,進入可運行狀態
notifyAll()方法可以使所有正在等待隊列中等待同一共享資源的全部線程從等待狀態退出,進入可運行狀態
最后,如果wait()方法和notify()/notifyAll()方法不在同步方法/同步代碼塊中被調用,那么虛擬機會拋出java.lang.IllegalMonitorStateException,注意一下。
wait()釋放鎖以及notify()不釋放鎖
多線程的學習中,任何地方都要關注"鎖",wait()和notify()也是這樣。wait()方法是釋放鎖的,寫一個例子來證明一下:
public class ThreadDomain31 { public void testMethod(Object lock) { try { synchronized (lock) { System.out.println(Thread.currentThread().getName() + " Begin wait()"); lock.wait(); System.out.println(Thread.currentThread().getName() + " End wait"); } } catch (InterruptedException e) { e.printStackTrace(); } } }
public class MyThread31 extends Thread { private Object lock; public MyThread31(Object lock) { this.lock = lock; } public void run() { ThreadDomain31 td = new ThreadDomain31(); td.testMethod(lock); } }
main函數調用一下:
public static void main(String[] args) { Object lock = new Object(); MyThread31 mt0 = new MyThread31(lock); MyThread31 mt1 = new MyThread31(lock); mt0.start(); mt1.start(); }
看一下運行結果:
Thread-0 Begin wait() Thread-1 Begin wait()
如果wait()方法不釋放鎖,那么Thread-1根本不會進入同步代碼塊打印的,所以,證明完畢。
接下來證明一下notify()方法不釋放鎖的結論:
public class ThreadDomain32 { public void testMethod(Object lock) { try { synchronized (lock) { System.out.println("Begin wait(), ThreadName = " + Thread.currentThread().getName()); lock.wait(); System.out.println("End wait(), ThreadName = " + Thread.currentThread().getName()); } } catch (InterruptedException e) { e.printStackTrace(); } } public void synNotifyMethod(Object lock) { try { synchronized (lock) { System.out.println("Begin notify(), ThreadName = " + Thread.currentThread().getName()); lock.notify(); Thread.sleep(5000); System.out.println("End notify(), ThreadName = " + Thread.currentThread().getName()); } } catch (InterruptedException e) { e.printStackTrace(); } } }
寫兩個線程分別調用2個方法:
public class MyThread32_0 extends Thread { private Object lock; public MyThread32_0(Object lock) { this.lock = lock; } public void run() { ThreadDomain32 td = new ThreadDomain32(); td.testMethod(lock); } }
public class MyThread32_1 extends Thread { private Object lock; public MyThread32_1(Object lock) { this.lock = lock; } public void run() { ThreadDomain32 td = new ThreadDomain32(); td.synNotifyMethod(lock); } }
寫個main函數調用一下:
public static void main(String[] args) throws Exception { Object lock = new Object(); MyThread32_0 mt0 = new MyThread32_0(lock); mt0.start(); MyThread32_1 mt1 = new MyThread32_1(lock); mt1.start(); MyThread32_1 mt2 = new MyThread32_1(lock); mt2.start(); }
看一下運行結果:
Begin wait(), ThreadName = Thread-0 Begin notify(), ThreadName = Thread-1 End notify(), ThreadName = Thread-1 Begin notify(), ThreadName = Thread-2 End notify(), ThreadName = Thread-2 End wait(), ThreadName = Thread-0
如果notify()方法釋放鎖,那么在Thread-1調用notify()方法后Thread.sleep(5000)必定應該有其他線程可以進入同步代碼塊了,但是實際上沒有,必須等到Thread-1把代碼執行完。所以,證明完畢。
interrupt()打斷wait()
之前有說過,interrupt()方法的作用不是中斷線程,而是在線程阻塞的時候給線程一個中斷標識,表示該線程中斷。wait()就是"阻塞的一種場景",看一下用interrupt()打斷wait()的例子:
public class ThreadDomain33 { public void testMethod(Object lock) { try { synchronized (lock) { System.out.println("Begin wait()"); lock.wait(); System.out.println("End wait()"); } } catch (InterruptedException e) { System.out.println("wait()被interrupt()打斷了!"); e.printStackTrace(); } } }
public class MyThread33 extends Thread { private Object lock; public MyThread33(Object lock) { this.lock = lock; } public void run() { ThreadDomain33 td = new ThreadDomain33(); td.testMethod(lock); } }
public static void main(String[] args) throws Exception { Object lock = new Object(); MyThread33 mt = new MyThread33(lock); mt.start(); Thread.sleep(5000); mt.interrupt(); }
看一下運行結果:
Begin wait() wait()被interrupt()打斷了! java.lang.InterruptedException at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:485) at com.xrq.example.e33.ThreadDomain33.testMethod(ThreadDomain33.java:12) at com.xrq.example.e33.MyThread33.run(MyThread33.java:15)
notifyAll()喚醒所有線程
利用Object對象的notifyAll()方法可以喚醒處於同一監視器下的所有處於wait的線程,舉個例子證明一下:
public class ThreadDomain34 { public void testMethod(Object lock) { try { synchronized (lock) { System.out.println("Begin wait(), ThreadName = " + Thread.currentThread().getName()); lock.wait(); System.out.println("End wait(), ThreadName = " + Thread.currentThread().getName()); } } catch (InterruptedException e) { e.printStackTrace(); } } }
寫兩個線程,一個調用testMethod(Object lock)的線程,一個notifyAll()線程:
public class MyThread34_0 extends Thread { private Object lock; public MyThread34_0(Object lock) { this.lock = lock; } public void run() { ThreadDomain34 td = new ThreadDomain34(); td.testMethod(lock); } }
public class MyThread34_1 extends Thread { private Object lock; public MyThread34_1(Object lock) { this.lock = lock; } public void run() { synchronized (lock) { lock.notifyAll(); } } }
main函數開三個wait線程,用一個notifyAll的線程去喚醒:
public static void main(String[] args) throws Exception { Object lock = new Object(); MyThread34_0 mt0 = new MyThread34_0(lock); MyThread34_0 mt1 = new MyThread34_0(lock); MyThread34_0 mt2 = new MyThread34_0(lock); mt0.start(); mt1.start(); mt2.start(); Thread.sleep(1000); MyThread34_1 mt3 = new MyThread34_1(lock); mt3.start(); }
看一下運行結果:
Begin wait(), ThreadName = Thread-0 Begin wait(), ThreadName = Thread-2 Begin wait(), ThreadName = Thread-1 End wait(), ThreadName = Thread-1 End wait(), ThreadName = Thread-2 End wait(), ThreadName = Thread-0
當然,喚醒的順序不重要,因為notifyAll()把處於同一資源下wait的線程全部喚醒,至於喚醒的順序,就和線程啟動的順序一樣,是虛擬機隨機的。