併發程式設計 多執行緒之間通訊

2021-09-18 07:01:24 字數 3638 閱讀 2070

多執行緒之間實現通訊

多執行緒之間如何實現通訊

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

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

畫圖演示

多執行緒之間通訊需求

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

**實現基本實現

共享資源源實體類

class res 

輸入執行緒資源

class intthrad extends thread

@override

public void run() else

count = (count + 1) % 2;

} }}

輸出執行緒

class outthread extends thread

@override

public void run()

}}

執行**

res res = new res();

intthrad intthrad = new intthrad(res);

outthread outthread = new outthread(res);

intthrad.start();

outthread.start();

執行**

注意:資料發生錯亂,造成執行緒安全問題

解決執行緒安全問題

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

}

wait、notify方法

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

}

wait與sleep區別

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

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

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

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

獲取物件鎖進入執行狀態。

lock鎖

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

lock寫法

lock lock  = new reentrantlock();

lock.lock();

tryfinally

lock與synchronized 關鍵字的區別

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

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

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

condition用法

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

}

併發程式設計 執行緒之間通訊

是同乙個鎖的資源。wait釋放鎖,notify不釋放鎖 public void runbase catch interruptedexception e start new thread start public void run02 catch interruptedexception e fin...

併發程式設計專題之多執行緒之間實現通訊

多執行緒之間通訊,其實就是多個執行緒在操作同乙個資源,但是操作的動作不同。需求 第乙個執行緒寫入 input 使用者,另乙個執行緒取讀取 out 使用者.實現讀乙個,寫乙個操作。共享資源源實體類,兩個執行緒將會對類中變數進行讀和寫操作 class res寫執行緒資源 class intthrad e...

多執行緒之間通訊

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