MonitorObject設計模式入手探索Java同步機制二.ppt_第1頁
MonitorObject設計模式入手探索Java同步機制二.ppt_第2頁
MonitorObject設計模式入手探索Java同步機制二.ppt_第3頁
MonitorObject設計模式入手探索Java同步機制二.ppt_第4頁
MonitorObject設計模式入手探索Java同步機制二.ppt_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

Monitor Object設計模式入手 探索Java同步機制 二,Monitor Object 設計模式 C+ 描述 我們將從以下幾個方面來討論 Monitor Object 模式。 問題描述 我們在開發(fā)并發(fā)的應用時,經常需要設計這樣的對象,該對象的方法會在多線程的環(huán)境下被調用,而這些方法的執(zhí)行都會改變該對象本身的狀態(tài)。為了防止競爭條件 (race condition) 的出現,對于這類對象的設計,需要考慮解決以下問題: 在任一時間內,只有唯一的公共的成員方法,被唯一的線程所執(zhí)行。 對于對象的調用者來說,如果總是需要在調用方法之前進行拿鎖,而在調用方法之后進行放鎖,這將會使并發(fā)應用編程變得更加困難。合理的設計是,該對象本身確保任何針對它的方法請求的同步被透明的進行,而不需要調用者的介入。,如果一個對象的方法執(zhí)行過程中,由于某些條件不能滿足而阻塞,應該允許其它的客戶端線程的方法調用可以訪問該對象。 我們使用 Monitor Object 設計模式來解決這類問題:將被客戶線程并發(fā)訪問的對象定義為一個 monitor 對象??蛻艟€程僅僅通過 monitor 對象的同步方法才能使用 monitor 對象定義的服務。為了防止陷入競爭條件,在任一時刻只能有一個同步方法被執(zhí)行。每一個 monitor 對象包含一個 monitor 鎖,被同步方法用于串行訪問對象的行為和狀態(tài)。此外,同步方法可以根據一個或多個與 monitor 對象相關的 monitor conditions 來決定在何種情況下掛起或恢復他們的執(zhí)行。,結構 在 Monitor Object 模式中,主要有四種類型的參與者: 監(jiān)視者對象 (Monitor Object): 負責定義公共的接口方法,這些公共的接口方法會在多線程的環(huán)境下被調用執(zhí)行。 同步方法:這些方法是監(jiān)視者對象所定義。為了防止競爭條件,無論是否同時有多個線程并發(fā)調用同步方法,還是監(jiān)視者對象含有多個同步方法,在任一時間內只有監(jiān)視者對象的一個同步方法能夠被執(zhí)行。 監(jiān)視鎖 (Monitor Lock): 每一個監(jiān)視者對象都會擁有一把監(jiān)視鎖。 監(jiān)視條件 (Monitor Condition): 同步方法使用監(jiān)視鎖和監(jiān)視條件來決定方法是否需要阻塞或重新執(zhí)行。,執(zhí)行序列圖 在監(jiān)視者對象模式中,在參與者之間將發(fā)生如下的協作過程: 1、同步方法的調用和串行化。當客戶線程調用監(jiān)視者對象的同步方法時,必須首先獲取它的監(jiān)視鎖。只要該監(jiān)視者對象有其他同步方法正在被執(zhí)行,獲取操作便不會成功。在這種情況下,客戶線程將被阻塞直到它獲取監(jiān)視鎖。當客戶線程成功獲取監(jiān)視鎖后,進入臨界區(qū),執(zhí)行方法實現的服務。一旦同步方法完成執(zhí)行,監(jiān)視鎖會被自動釋放,目的是使其他客戶線程有機會調用執(zhí)行該監(jiān)視者對象的同步方法。 2、同步方法線程掛起。如果調用同步方法的客戶線程必須被阻塞或是有其他原因不能立刻進行,它能夠在一個監(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)視者對象設計模式來解決一個實際的問題。 這是一個典型的生產者 / 消費者模式問題。假定我們有一個固定長度的消息隊列,該隊列會被多個生產者 / 消費者線程所操作,生產者線程負責將消息放入該隊列,而消費者線程負責從該對列中取出消息。 清單 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論