




已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
Monitor Object設計模式入手 探索Java同步機制 二,Monitor Object 設計模式 C+ 描述 我們將從以下幾個方面來討論 Monitor Object 模式。 問題描述 我們在開發(fā)并發(fā)的應用時,經(jīng)常需要設計這樣的對象,該對象的方法會在多線程的環(huán)境下被調(diào)用,而這些方法的執(zhí)行都會改變該對象本身的狀態(tài)。為了防止競爭條件 (race condition) 的出現(xiàn),對于這類對象的設計,需要考慮解決以下問題: 在任一時間內(nèi),只有唯一的公共的成員方法,被唯一的線程所執(zhí)行。 對于對象的調(diào)用者來說,如果總是需要在調(diào)用方法之前進行拿鎖,而在調(diào)用方法之后進行放鎖,這將會使并發(fā)應用編程變得更加困難。合理的設計是,該對象本身確保任何針對它的方法請求的同步被透明的進行,而不需要調(diào)用者的介入。,如果一個對象的方法執(zhí)行過程中,由于某些條件不能滿足而阻塞,應該允許其它的客戶端線程的方法調(diào)用可以訪問該對象。 我們使用 Monitor Object 設計模式來解決這類問題:將被客戶線程并發(fā)訪問的對象定義為一個 monitor 對象??蛻艟€程僅僅通過 monitor 對象的同步方法才能使用 monitor 對象定義的服務。為了防止陷入競爭條件,在任一時刻只能有一個同步方法被執(zhí)行。每一個 monitor 對象包含一個 monitor 鎖,被同步方法用于串行訪問對象的行為和狀態(tài)。此外,同步方法可以根據(jù)一個或多個與 monitor 對象相關(guān)的 monitor conditions 來決定在何種情況下掛起或恢復他們的執(zhí)行。,結(jié)構(gòu) 在 Monitor Object 模式中,主要有四種類型的參與者: 監(jiān)視者對象 (Monitor Object): 負責定義公共的接口方法,這些公共的接口方法會在多線程的環(huán)境下被調(diào)用執(zhí)行。 同步方法:這些方法是監(jiān)視者對象所定義。為了防止競爭條件,無論是否同時有多個線程并發(fā)調(diào)用同步方法,還是監(jiān)視者對象含有多個同步方法,在任一時間內(nèi)只有監(jiān)視者對象的一個同步方法能夠被執(zhí)行。 監(jiān)視鎖 (Monitor Lock): 每一個監(jiān)視者對象都會擁有一把監(jiān)視鎖。 監(jiān)視條件 (Monitor Condition): 同步方法使用監(jiān)視鎖和監(jiān)視條件來決定方法是否需要阻塞或重新執(zhí)行。,執(zhí)行序列圖 在監(jiān)視者對象模式中,在參與者之間將發(fā)生如下的協(xié)作過程: 1、同步方法的調(diào)用和串行化。當客戶線程調(diào)用監(jiān)視者對象的同步方法時,必須首先獲取它的監(jiān)視鎖。只要該監(jiān)視者對象有其他同步方法正在被執(zhí)行,獲取操作便不會成功。在這種情況下,客戶線程將被阻塞直到它獲取監(jiān)視鎖。當客戶線程成功獲取監(jiān)視鎖后,進入臨界區(qū),執(zhí)行方法實現(xiàn)的服務。一旦同步方法完成執(zhí)行,監(jiān)視鎖會被自動釋放,目的是使其他客戶線程有機會調(diào)用執(zhí)行該監(jiān)視者對象的同步方法。 2、同步方法線程掛起。如果調(diào)用同步方法的客戶線程必須被阻塞或是有其他原因不能立刻進行,它能夠在一個監(jiān)視條件上等待,這將導致該客戶線程暫時釋放監(jiān)視鎖,并被掛起在監(jiān)視條件上。,3、監(jiān)視條件通知。一個客戶線程能夠通知一個監(jiān)視條件,目的是為了讓一個前期使自己掛起在一個監(jiān)視條件上的同步方法線程恢復運行。 4、同步方法線程恢復。一旦一個早先被掛起在監(jiān)視條件上的同步方法線程獲取通知,它將繼續(xù)在最初的等待監(jiān)視條件的點上執(zhí)行。在被通知線程被允許恢復執(zhí)行同步方法之前,監(jiān)視鎖將自動被獲取。圖 1 描述了監(jiān)視者對象的動態(tài)特性。,示例 在本節(jié)中,我們將使用監(jiān)視者對象設計模式來解決一個實際的問題。 這是一個典型的生產(chǎn)者 / 消費者模式問題。假定我們有一個固定長度的消息隊列,該隊列會被多個生產(chǎn)者 / 消費者線程所操作,生產(chǎn)者線程負責將消息放入該隊列,而消費者線程負責從該對列中取出消息。 清單 6. Message_Queue.h class Message_Queue public: enum MAX_MESSAGES = 100/* . */ ; / The constructor defines the maximum number / of messages in the queue. This determines when the queue is full. Message_Queue(size_t max_messages = MAX_MESSAGES); virtual Message_Queue();,/ Put the at the tail of the queue. / If the queue is full, block until the queue is not full. /* synchronized */ void put (const Message ,private: / Put the at the tail of the queue, and / get the at its head, respectively. / Note that, the internal methods are not synchronized. void put_i (const Message ,/ The maximum number s that can be / in a queue before its considered full. size_t max_messages_; / Monitor lock that protects the queues / internal state from race conditions during concurrent access. mutable Thread_Mutex monitor_lock_; / Condition variable used in conjunction with to make / synchronized method threads wait until the queue is no longer empty. Thread_Condition not_empty_; / Condition variable used in conjunction with to make / synchronized method threads wait until the queue is no longer full. Thread_Condition not_full_; ;,清單 7. Message_Queue.cpp #include “Message_Queue.h“ Message_Queue:Message_Queue (size_t max_messages) :not_full_(monitor_lock_), not_empty_(monitor_lock_), max_messages_(max_messages), message_count_(0) bool Message_Queue:empty () const Guard guard (monitor_lock_); return empty_i (); bool Message_Queue:full () const,Guard guard (monitor_lock_); return full_i (); void Message_Queue:put (const Message ,/ Enqueue the at the tail. put_i (msg); / Notify any thread waiting in that the queue has at least one . not_empty_.notify (); / Destructor of releases . Message Message_Queue:get () / Use the Scoped Locking idiom to acquire/release the upon / entry/exit to the synchronized method. Guard guard (monitor_lock_); / Wait while the queue is empty. while (empty_i () / Release and suspend the / calling thread waiting for a new to / be put into the queue. The is / reacquired automatically when returns. not_empty_.wait (); ,/ Dequeue the first in the queue and update the . Message m = get_i (); / Notify any thread waiting in that the / queue has room for at least one . not_full_.notify (); return
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 能源采購合同終止及替代能源及節(jié)能減排協(xié)議
- 黨風廉政責任追究與保障機制合同
- 訂購飼料協(xié)議書范本
- 編織袋出口退稅與政策支持合作協(xié)議
- 委托撫養(yǎng)病人協(xié)議書范本
- 車輛租賃合同事故處理補充協(xié)議范本
- 餐廳廣告宣傳合作協(xié)議范本
- 鋼混組合梁剪力釘焊接技術(shù)
- 餐飲服務案例范文(21篇)
- 2024勞動關(guān)系終止和解除終止解除勞動合同(模板7篇)
- 肢體離斷傷的護理
- 浙江省強基聯(lián)盟學考模擬2024-2025學年高二下學期6月學考模擬地理試題(含答案)
- 中國美術(shù)學院非教學崗位招聘筆試真題2024
- JIS G4305-2021 冷軋不銹鋼板材、薄板材和帶材
- 危險化學品臨界量表(參考)
- 墻柱梁板混凝土同時澆筑方案.doc
- 新生兒視覺訓練黑白卡(整理90張必備圖卡)
- 礦山地質(zhì)環(huán)境恢復治理方案治理經(jīng)費估算計算部分
- 大學遺傳學期末考試題庫及答案參考
- 藝術(shù)設計專業(yè)“職場化”工作室制實踐教學模式論文
- 工程機械銷售基本知識.ppt
評論
0/150
提交評論