前言:
關于等待/通知,要記住的關鍵點是:
必須從同步環境內調用wait()、notify()、notifyAll()方法。線程不能調用對象上等待或通知的方法,除非它擁有那個對象的鎖。
wait()、notify()、notifyAll()都是Object的實例方法。與每個對象具有鎖一樣,每個對象可以有一個線程列表,他們等待來自該信號(通知)。線程通過執行對象上的wait()方法獲得這個等待列表。從那時候起,它不再執行任何其他指令,直到調用對象的notify()方法為止。如果多個線程在同一個對象上等待,則將只選擇一個線程(不保證以何種順序)繼續執行。如果沒有線程等待,則不采取任何特殊操作。
示例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package threadwait; public class ThreadA extends Thread{ public int num = 0 ; public void run(){ synchronized ( this ){ //在此類對象上實現同步,this指代當前對象 for ( int i = 0 ; i < 3 ; ++i) this .num+=i; notifyAll(); //通知所有在這個對象上等待的線程開始執行,在這里就是通知TestNotify主線程開始執行 } } public int getNum(){ return this .num; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package threadwait; public class TestNotify{ public static void main(String args[]){ ThreadA threada = new ThreadA(); threada.start(); //threada線程有執行的資格,但是還沒有開始執行 synchronized (threada){ try { threada.wait(); //主線程等待threada線程執行結束才開始執行 //而且只有獲得了當前threada對象的鎖之后才能執行wait,就是說在同步域內才可以執行wait,執行wait后放棄對象鎖 } catch (InterruptedException e){ e.printStackTrace(); } } System.out.println(threada.getNum()); } } |
同步可以是在class級別上的,synchronized(A.class),也可以是在對象級別上的synchronized(this),可以是靜態同步方法,static synchronized ,靜態同步方法是在class級別上的,非靜態同步方法是在類對象級別上的,一個類對象只有一個鎖,只有獲得了該鎖才可以對他執行wait操作,后釋放掉該鎖。
更進一步的實例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package threadwait; public class ThreadA extends Thread{ public int num = 0 ; public void run(){ synchronized ( this ){ //在此類對象上實現同步,this指代當前對象 for ( int i = 0 ; i < 3 ; ++i) this .num+=i; try { Thread.sleep( 500 ); //如果ThreadB的三個示例線程在還沒有進入等待狀態之前就受到了notifyall的信號 //那將會發生嚴重后果,因為調用notifyall的線程只可以調用一次notifyall,那造成等待的線程將永遠等待下去 //所以在此處讓它睡一小會,讓其他線程有時間進入等待狀態。 //不然會收到 } catch (InterruptedException e){ e.printStackTrace(); } notifyAll(); //通知所有在這個對象上等待的線程開始執行,在這里就是通知TestNotify主線程開始執行 } // notifyAll(); } public int getNum(){ return this .num; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package threadwait; public class ThreadB extends Thread{ private ThreadA threada; public ThreadB(ThreadA ta){ this .threada = ta; } public void run(){ System.out.println(Thread.currentThread().getName()+ " is waitting." ); synchronized (threada){ try { threada.wait(); } catch (InterruptedException e){ e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+ " " + this .threada.getNum()); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package threadwait; public class TestNotify{ public static void main(String args[]){ ThreadA threada = new ThreadA(); new ThreadB(threada).start(); new ThreadB(threada).start(); new ThreadB(threada).start(); threada.start(); } } |
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/alwyq/article/details/36033719