java 中線(xiàn)程等待與通知的實(shí)現(xiàn)
前言:
關(guān)于等待/通知,要記住的關(guān)鍵點(diǎn)是:
必須從同步環(huán)境內(nèi)調(diào)用wait()、notify()、notifyAll()方法。線(xiàn)程不能調(diào)用對(duì)象上等待或通知的方法,除非它擁有那個(gè)對(duì)象的鎖。
wait()、notify()、notifyAll()都是Object的實(shí)例方法。與每個(gè)對(duì)象具有鎖一樣,每個(gè)對(duì)象可以有一個(gè)線(xiàn)程列表,他們等待來(lái)自該信號(hào)(通知)。線(xiàn)程通過(guò)執(zhí)行對(duì)象上的wait()方法獲得這個(gè)等待列表。從那時(shí)候起,它不再執(zhí)行任何其他指令,直到調(diào)用對(duì)象的notify()方法為止。如果多個(gè)線(xiàn)程在同一個(gè)對(duì)象上等待,則將只選擇一個(gè)線(xiàn)程(不保證以何種順序)繼續(xù)執(zhí)行。如果沒(méi)有線(xiàn)程等待,則不采取任何特殊操作。
示例代碼:
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 ){ //在此類(lèi)對(duì)象上實(shí)現(xiàn)同步,this指代當(dāng)前對(duì)象 for ( int i = 0 ; i < 3 ; ++i) this .num+=i; notifyAll(); //通知所有在這個(gè)對(duì)象上等待的線(xiàn)程開(kāi)始執(zhí)行,在這里就是通知TestNotify主線(xiàn)程開(kāi)始執(zhí)行 } } 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線(xiàn)程有執(zhí)行的資格,但是還沒(méi)有開(kāi)始執(zhí)行 synchronized (threada){ try { threada.wait(); //主線(xiàn)程等待threada線(xiàn)程執(zhí)行結(jié)束才開(kāi)始執(zhí)行 //而且只有獲得了當(dāng)前threada對(duì)象的鎖之后才能執(zhí)行wait,就是說(shuō)在同步域內(nèi)才可以執(zhí)行wait,執(zhí)行wait后放棄對(duì)象鎖 } catch (InterruptedException e){ e.printStackTrace(); } } System.out.println(threada.getNum()); } } |
同步可以是在class級(jí)別上的,synchronized(A.class),也可以是在對(duì)象級(jí)別上的synchronized(this),可以是靜態(tài)同步方法,static synchronized ,靜態(tài)同步方法是在class級(jí)別上的,非靜態(tài)同步方法是在類(lèi)對(duì)象級(jí)別上的,一個(gè)類(lèi)對(duì)象只有一個(gè)鎖,只有獲得了該鎖才可以對(duì)他執(zhí)行wait操作,后釋放掉該鎖。
更進(jìn)一步的實(shí)例代碼如下:
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 ){ //在此類(lèi)對(duì)象上實(shí)現(xiàn)同步,this指代當(dāng)前對(duì)象 for ( int i = 0 ; i < 3 ; ++i) this .num+=i; try { Thread.sleep( 500 ); //如果ThreadB的三個(gè)示例線(xiàn)程在還沒(méi)有進(jìn)入等待狀態(tài)之前就受到了notifyall的信號(hào) //那將會(huì)發(fā)生嚴(yán)重后果,因?yàn)檎{(diào)用notifyall的線(xiàn)程只可以調(diào)用一次notifyall,那造成等待的線(xiàn)程將永遠(yuǎn)等待下去 //所以在此處讓它睡一小會(huì),讓其他線(xiàn)程有時(shí)間進(jìn)入等待狀態(tài)。 //不然會(huì)收到 } catch (InterruptedException e){ e.printStackTrace(); } notifyAll(); //通知所有在這個(gè)對(duì)象上等待的線(xiàn)程開(kāi)始執(zhí)行,在這里就是通知TestNotify主線(xiàn)程開(kāi)始執(zhí)行 } // 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(); } } |
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
原文鏈接:http://blog.csdn.net/alwyq/article/details/36033719