4 多執行緒之間實現通訊

2021-09-29 21:05:05 字數 3938 閱讀 6370

目錄

知識點1:多執行緒之間如何實現通訊

1、什麼是多執行緒之間通訊?

2、多執行緒之間通訊需求

3、**實現基本實現

(1)共享資源源實體類

(2)輸入執行緒資源

(3)輸出執行緒

(4)執行**

(5)解決執行緒安全問題

知識點2:wait、notify方法

知識點3:wait與sleep區別

知識點4:lock鎖

1、lock寫法

2、lock與synchronized 關鍵字的區別

3、condition用法

多執行緒之間通訊,其實就是多個執行緒在操作同乙個資源,但是操作的動作不同。

畫圖演示

需求:第乙個執行緒寫入(input)使用者,另乙個執行緒取讀取(out)使用者.實現讀乙個,寫乙個操作。

intthrad 加上synchronized

class intthrad extends thread 

@override

public void run() else

count = (count + 1) % 2;

}} }

}

輸出執行緒加上synchronized

class res 

class inputthread extends thread

@override

public void run() else

count = (count + 1) % 2;

} } }}

class outthrad extends thread

@override

public void run()

} }}public class threaddemo01

}

1.因為涉及到物件鎖,他們必須都放在synchronized中來使用. wait、notify一定要在synchronized裡面進行使用。

2.wait必須暫定當前正在執行的執行緒,並釋放資源鎖,讓其他執行緒可以有機會執行

3. notify/notifyall: 喚醒因鎖池中的執行緒,使之執行

class res
class intthrad extends thread 

@override

public void run() catch (exception e)

}if (count == 0) else

count = (count + 1) % 2;

res.flag = true;

// 喚醒當前執行緒

res.notify();

}} }

}

class outthread extends thread 

@override

public void run() catch (exception e)

}system.out.println(res.username + "--" + res.user***);

res.flag = false;

res.notify();

}} }

}

public class threacommun 

}

對於sleep()方法,我們首先要知道該方法是屬於thread類中的。而wait()方法,則是屬於object類中的。

sleep()方法導致了程式暫停執行指定的時間,讓出cpu該其他執行緒,但是他的監控狀態依然保持者,當指定的時間到了又會自動恢復執行狀態。

在呼叫sleep()方法的過程中,執行緒不會釋放物件鎖。

而當呼叫wait()方法的時候,執行緒會放棄物件鎖,進入等待此物件的等待鎖定池,只有針對此物件呼叫notify()方法後本執行緒才進入物件鎖定池準備獲取物件鎖進入執行狀態。

在 jdk1.5 之後,並發包中新增了 lock 介面(以及相關實現類)用來實現鎖功能,lock 介面提供了與 synchronized 關鍵字類似的同步功能,但需要在使用時手動獲取鎖和釋放鎖。

lock lock  = new reentrantlock();

lock.lock();

tryfinally

lock 介面可以嘗試非阻塞地獲取鎖 當前執行緒嘗試獲取鎖。如果這一時刻鎖沒有被其他執行緒獲取到,則成功獲取並持有鎖。

lock 介面能被中斷地獲取鎖 與 synchronized 不同,獲取到鎖的執行緒能夠響應中斷,當獲取到的鎖的執行緒被中斷時,中斷異常將會被丟擲,同時鎖會被釋放。

lock 介面在指定的截止時間之前獲取鎖,如果截止時間到了依舊無法獲取鎖,則返回。

condition的功能類似於在傳統的執行緒技術中的,object.wait()和object.notify()的功能。

condition condition = lock.newcondition();

res. condition.await(); 類似wait

res. condition. signal() 類似notify

class res 

class inputthread extends thread

@override

public void run() catch (exception e)

}if (count == 0) else

count = (count + 1) % 2;

res.flag = true;

// res.notify();

newcondition.signal();

} catch (exception e) finally

} // } }}

class outthrad extends thread

@override

public void run() catch (exception e)

}system.out.println(res.username + "," + res.***);

res.flag = false;

// res.notify();

newcondition.signal();

} catch (exception e) finally

// }

} }}public class threaddemo01

}

多執行緒之間通訊

多執行緒之間通訊,其實就是多個執行緒在操作同乙個資源,但是操作的動作不同。需求 第乙個執行緒寫入 input 使用者,另乙個執行緒取讀取 out 使用者.實現讀乙個,寫乙個操作。共享資源源實體類 class res輸入執行緒資源 class intthrad extends thread overr...

多執行緒之間通訊

多執行緒之間通訊 就是多個執行緒在操作同乙個資源,但是操作的動作不同 現在需要實現,生產一台電機,銷售一台電機問題。實現 執行結果 資料發生錯亂,造成執行緒安全問題 解決執行緒安全問題 通過wait notify來解決。wait和sleep的區別 wait可以指定時間也可以不指定時間,sleep必須...

多執行緒學習3(多執行緒之間實現通訊)

多執行緒之間通訊,其實就是多個執行緒在操作同乙個資源,但是操作的動作不同。畫圖演示 需求 第乙個執行緒寫入 input 使用者,另乙個執行緒取讀取 out 使用者.實現讀乙個,寫乙個操作。實現 共享資源 class user 輸入執行緒 class out extends thread overri...