版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、并發(fā)型設(shè)計(jì)模式并發(fā)型設(shè)計(jì)模式并發(fā)型設(shè)計(jì)模式并發(fā)型設(shè)計(jì)模式 常見(jiàn)并發(fā)型設(shè)計(jì)模式 Monitor Object設(shè)計(jì)模式 Half-Sync/Half-Async設(shè)計(jì)模式 Leader/Followers設(shè)計(jì)模式 Active Object設(shè)計(jì)模式 Thread-Specific Storage 設(shè)計(jì)模式The Monitor Object PatternThis pattern synchronizes concurrent method execution to ensure that only one method at a time runs within an objectIt also
2、allows an objects methods to cooperatively schedule their execution sequences2.*usesuses *Monitor ObjectMonitor Objectsync_method1()sync_methodN()Monitor LockMonitor Lockacquire()release()ClientClientMonitor ConditionMonitor Conditionwait()notify()notify_all()SolutionApply the Monitor Object design
3、pattern (P2) to synchronize the queue efficiently & conveniently Its instructive to compare Monitor Object pattern solutions with Active Object pattern solutionsThe key tradeoff is efficiency vs. flexibilityMonitor Object Pattern Dynamics: MonitorObject: MonitorLock: MonitorConditionsync_method1
4、()wait()dowork(): ClientThread1: ClientThread2acquire()dowork()acquire()sync_method2()release()notify()dowork()release()the OS thread schedulerautomatically suspendsthe client threadthe OS threadschedulerautomatically resumes the clientthread & thesynchronizedmethodthe OS thread scheduleratomica
5、lly reacquiresthe monitor lockthe OS thread scheduleratomically releasesthe monitor lock1.Synchronized method invocation & serialization2.Synchronized method thread suspension3.Monitor condition notification4.Synchronized method thread resumptionThe Half-Sync/Half-Async PatternSyncServiceLayerAs
6、yncService LayerQueueingLayerSync Service 1Sync Service 2Sync Service 3ExternalEvent SourceQueueAsync ServiceThe Half-Sync/Half-Async architectural pattern decouples async & sync service processing in concurrent systems, to simplify programming without unduly reducing performanceSolutionApply th
7、e Half-Sync/Half-Async architectural pattern (P2) to scale up server performance by processing different HTTP requests concurrently in multiple threads This solution yields two benefits:1.Threads can be mapped to separate CPUs to scale up server performance via multi-processing2.Each thread blocks i
8、ndependently, which prevents a flow-controlled connection from degrading the QoS that other clients receiveThis pattern defines two service processing layersone async & one syncalong with a queueing layer that allows services to exchange messages between the two layers: External EventSource: Asy
9、nc Service: Queuenotificationread()enqueue()message: Sync Servicework()messageread()messagework()notificationHalf-Sync/Half-Async Pattern DynamicsThe pattern allows sync services, such as HTTP protocol processing, to run concurrently, relative both to each other & to async services, such as even
10、t demultiplexingThe Leader/Followers PatternThis pattern eliminates the need for& the overhead ofa separate Reactor thread & synchronized request queue used in the Half-Sync/Half-Async patternThe Leader/Followers architectural pattern (P2) provides an efficient concurrency model where multip
11、le threads take turns sharing event sources to detect, demux, dispatch, & process service requests that occur on the event sourcesHandlesHandle SetsConcurrent HandlesIterative HandlesConcurrent Handle SetsUDP Sockets + WaitForMultipleObjects()TCP Sockets + WaitForMultpleObjects()Iterative Handle
12、 SetsUDP Sockets + select()/poll()TCP Sockets + select()/poll()Handleusesdemultiplexes*Handle Sethandle_events()deactivate_handle()reactivate_handle()select()Event Handlerhandle_event ()get_handle()Concrete Event Handler Bhandle_event ()get_handle()Concrete Event Handler Ahandle_event ()get_handle()
13、Thread Pooljoin()promote_new_leader()synchronizerLeader/Followers Pattern Dynamics: ConcreteEvent Handlerjoin()handle_event(): ThreadPool : HandleSetjoin()thread2 sleepsuntil it becomesthe leadereventthread1 sleepsuntil it becomesthe leaderdeactivate_handle()join()Thread1Thread2handle_events()reacti
14、vate_handle()handle_event()eventthread2waits for anew event,thread1processescurrenteventdeactivate_handle()handle_events()new_leader()1.Leader thread demuxing2.Follower thread promotion3.Event handler demuxing & event processing4.Rejoining the thread poolpromote_Supporting OO Async Communication
15、 (1/2)ContextSome clients want to invoke remote operations, continue their work, & retrieve the results at a later point in timeProblemUsing the explicit message-passing API of the Messaging pattern can reduce type-safety & performanceSimilar to motivation for Proxy pattern.ClientServerReque
16、st messagesRequest messagesReply messagesSupporting OO Async Communication (2/2)SolutionApply the Active Object design pattern (P2) to decouple method invocation from method execution using a strongly-typed OO programming modelA proxy provides an interface that allows clients to access methods of an
17、 objectA concrete method request is created for every method invoked on the proxyA scheduler receives the method requests & dispatches them on the servant when they become runnableAn activation list maintains pending method requestsA servant implements the methodsA future allows clients to acces
18、s the results of a method call on the proxyFutureSchedulerenqueue dispatchMethodRequestguardcall*Proxymethod_1method_nActivationListenqueuedequeueServantmethod_1method_ncreatescreatesmaintainsConcreteMethodRequest1ConcreteMethodRequest2A client invokes a method on the proxyThe proxy returns a future
19、 to the client, & creates a method request, which it passes to the schedulerThe scheduler enqueues the method request into the activation list (not shown here)When the method request becomes runnable, the scheduler dequeues it from the activation list (not shown here) & executes it in a different thread than the clientThe method request executes the method on the servant & writes results, if any, to the futureClients obtain the methods results v
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度綠色餐飲采購(gòu)標(biāo)準(zhǔn)合同3篇
- 二零二五年度冷鏈物流倉(cāng)儲(chǔ)管理服務(wù)合同2篇
- 2025年度苗木種植基地土地租賃合同樣本(含品牌授權(quán))
- 2025年度飛行員勞動(dòng)合同(含飛行業(yè)績(jī)獎(jiǎng)勵(lì))4篇
- 中醫(yī)師專(zhuān)屬2024聘用協(xié)議模板版B版
- 個(gè)性化全新承諾協(xié)議文檔(2024版)版B版
- 二零二五年度出租車(chē)公司股權(quán)置換及運(yùn)營(yíng)權(quán)轉(zhuǎn)讓協(xié)議3篇
- 2025年度個(gè)人商鋪?zhàn)赓U稅費(fèi)代繳及財(cái)務(wù)結(jié)算合同4篇
- 二零二五年度農(nóng)民合作社加盟社員入社合同范本
- 個(gè)人寵物寄養(yǎng)服務(wù)2024年度合同
- 皮膚內(nèi)科過(guò)敏反應(yīng)病例分析
- 電影《獅子王》的視聽(tīng)語(yǔ)言解析
- 妊娠合并低鉀血癥護(hù)理查房
- 煤礦反三違培訓(xùn)課件
- 向流程設(shè)計(jì)要效率
- 2024年中國(guó)航空發(fā)動(dòng)機(jī)集團(tuán)招聘筆試參考題庫(kù)含答案解析
- 當(dāng)代中外公司治理典型案例剖析(中科院研究生課件)
- 動(dòng)力管道設(shè)計(jì)手冊(cè)-第2版
- 2022年重慶市中考物理試卷A卷(附答案)
- Python繪圖庫(kù)Turtle詳解(含豐富示例)
- 煤礦機(jī)電設(shè)備檢修技術(shù)規(guī)范完整版
評(píng)論
0/150
提交評(píng)論