設(shè)計(jì)模式-10-并發(fā)型設(shè)計(jì)模式_第1頁(yè)
設(shè)計(jì)模式-10-并發(fā)型設(shè)計(jì)模式_第2頁(yè)
設(shè)計(jì)模式-10-并發(fā)型設(shè)計(jì)模式_第3頁(yè)
設(shè)計(jì)模式-10-并發(fā)型設(shè)計(jì)模式_第4頁(yè)
設(shè)計(jì)模式-10-并發(fā)型設(shè)計(jì)模式_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論