下面通過代碼給大家介紹java多線程通訊之wait notify的區(qū)別,具體內(nèi)容如下所示:
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
48
49
50
51
52
53
54
55
56
57
58
59
|
class res{ public string username; public string sex; } class out extends thread{ res res; public out(res res){ this .res=res; } @override public void run() { //寫操作 int count= 0 ; while ( true ){ // synchronized (res.getclass()){ if (count== 0 ){ //偶數(shù) res.username= "小明" ; res.sex= "男" ; } else { //奇數(shù) res.username= "小紅" ; res.sex= "女" ; } count=(count+ 1 )% 2 ; // } } } } class input extends thread{ res res; public input(res res){ this .res=res; } @override public void run() { while ( true ){ // synchronized (res.getclass()){ system.out.println(res.username+ "," +res.sex); // } } } } public class outinputthread { public static void main(string[] args) { res res = new res(); out out = new out(res); input input = new input(res); out.start(); input.start(); } } 小紅,女 小紅,女 小紅,女 小紅,女 小紅,女 小紅,女 小紅,女 小紅,女 小紅,女 |
出現(xiàn)以上結(jié)果??消費(fèi)者一直消費(fèi)或者生產(chǎn)者一直生產(chǎn)
解決方法:生產(chǎn)者生產(chǎn)完成后消費(fèi)者方可消費(fèi),否者不可消費(fèi),消費(fèi)者未消費(fèi)或者未消費(fèi)完生產(chǎn)者不可生產(chǎn),一次生產(chǎn)一次消費(fèi)。其實(shí)也就是保證對res共享資源的操作同一時刻僅有同一個線程進(jìn)行操作,
wait、notify、notifyall方法
wait、notify、notifyall是三個定義在object類里的方法,可以用來控制線程的狀態(tài)。
這三個方法最終調(diào)用的都是jvm級的native方法。隨著jvm運(yùn)行平臺的不同可能有些許差異。
如果對象調(diào)用了wait方法就會使持有該對象的線程把該對象的控制權(quán)交出去,然后處于等待狀態(tài)。當(dāng)前線程從運(yùn)行變?yōu)樽枞尫潘馁Y源
如果對象調(diào)用了notify方法就會通知某個正在等待這個對象的控制權(quán)的線程可以繼續(xù)運(yùn)行。讓持有該鎖的線程從阻塞態(tài)變?yōu)榫途w。
如果對象調(diào)用了notifyall方法就會通知所有等待這個對象控制權(quán)的線程繼續(xù)運(yùn)行。
注意:一定要在線程同步中使用,并且是同一個鎖的資源
通過以下方式即可完成需求。
生產(chǎn)者獲取res.getclass鎖后,如果flag為true生產(chǎn)者通過調(diào)用res.getclass.wait進(jìn)行等待,此時其他線程可獲取該鎖,如果flag為false,進(jìn)行生產(chǎn),然后設(shè)置flag為true保證資源消費(fèi)后方可再生產(chǎn),接著通過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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
class res{ public string username; public string sex; //true 生產(chǎn)者等待,消費(fèi)者可消費(fèi) false生產(chǎn)者可以生產(chǎn),消費(fèi)者不可消費(fèi) public boolean flag= false ; } class out extends thread{ res res; public out(res res){ this .res=res; } @override public void run() { //寫操作 int count= 0 ; while ( true ){ synchronized (res.getclass()){ if (res.flag){ try { res.getclass().wait(); //讓當(dāng)前線程從運(yùn)行變?yōu)樽枞⑶裔尫潘馁Y源 } catch (interruptedexception e) { e.printstacktrace(); } } if (count== 0 ){ //偶數(shù) res.username= "小明" ; res.sex= "男" ; } else { //奇數(shù) res.username= "小紅" ; res.sex= "女" ; } count=(count+ 1 )% 2 ; res.flag= true ; res.getclass().notify(); } } } } class input extends thread{ res res; public input(res res){ this .res=res; } @override public void run() { while ( true ){ synchronized (res.getclass()){ if (!res.flag){ try { res.getclass().wait(); } catch (interruptedexception e) { e.printstacktrace(); } } system.out.println(res.username+ "," +res.sex); res.flag= false ; res.getclass().notify(); } } } } public class outinputthread { public static void main(string[] args) { res res = new res(); out out = new out(res); input input = new input(res); out.start(); input.start(); } } |
輸出如下:
小明,男
小紅,女
小明,男
小紅,女
小明,男
小紅,女
小明,男
小紅,女
如果去掉notify會怎樣?去掉一個?去掉兩個?
去掉一個生產(chǎn)者可以打印多個(但是也不多),去掉消費(fèi)者僅可打印一個,去掉兩個可能不打印,也可能打印1個,所以wait、notify必須成對使用
wait(用于同步中)與sleep區(qū)別?
都是做休眠,wait需要notify
對于sleep方法,我們首先要知道該方法是屬于thread類中的。而wait方法,則是屬于object類中的。
sleep方法導(dǎo)致了程序暫停執(zhí)行指定的時間,讓出cpu該其他線程,但是他的監(jiān)控狀態(tài)依然保持者,當(dāng)指定的時間到了又會自動恢復(fù)運(yùn)行狀態(tài)。
在調(diào)用sleep方法的過程中,線程不會釋放對象鎖。
而當(dāng)調(diào)用wait方法的時候,線程會放棄對象鎖,進(jìn)入等待此對象的等待鎖定池,只有針對此對象調(diào)用notify方法后本線程才進(jìn)入對象鎖定池準(zhǔn)備獲取對象鎖進(jìn)入運(yùn)行狀態(tài)。
總結(jié)
以上所述是小編給大家介紹的java多線程通訊之wait,notify的區(qū)別詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://www.cnblogs.com/losemyfuture/archive/2018/07/24/9357846.html