55

Multithreading development with workers

Embed Size (px)

Citation preview

Aboutme• Front-enddeveloper• Mobiledeveloper

• AgileCoach/Mentor• CurrentlyworkinginCengage

WhatisMultithreading?Multi-ThreadingistheabilityofaCPUtoexecuteseveralthreadsofexecutionapparentlyatthesametime

Multi-threadingisawidespreadprogrammingandexecutionmodelthatallowsmultiplethreadstoexistwithinthecontextofasingleprocess

Multi~threading/tasking• Multitaskingisamethodbywhichmultipleprocessessharecommonprocessingresources• Multithreadingextendstheideaofmultitaskingintoapplications

Processes

8770mdworker0.000:00.114054756768K5760K0B0B104M2467M78770195sleeping

• Aprocessisaninstanceofaprograminexecution• Processesmaybeinoneoffivestates:New,Ready,Running,Waiting,Terminated

ThreadsAthreadisthesmallestsequenceofprogrammedinstructionsthatcanbemanagedindependentlybyanOSscheduler

KindofThreads• Userlevelthreads(youcanplaysafelywiththem)• Kernellevelthreads(lesssafelybutstillfunny!)• UserlevelthreadsareinthescopeofActionScriptandFlex

ThreadsandProcesses• Athreadiscontainedinsideaprocess• Multiplethreadscanexistwithinthesameprocessandshareresourcessuchasmemory

Differences(1/2)• Athreadislightweighttakinglesserresourcesthanaprocess• Processswitchingneedsinteractionwithoperatingsystemwhilethreadswitchingdoesnot• Eachprocessexecutesthesamecodeusingitsownresources,threadsusesameresources

Differences(2/2)• Whileonethreadisblockedandwaiting,secondthreadinthesametaskcanrun• Multipleprocessesusemoreresources,multiplethreadedprocessesusefewerresources• Processesoperateindependentlyoftheothers,threadsaccessotherthread'sdata

MultithreadingvsAsync• AsyncisthebetterindicatedfordoingheavyIOboundprocessing• Multithreadingisbetterindicatedwithlongrunningprocesses

TrivialParallelismScenario:largenumberofindependentcomputation-intensivetasks

• Createapoolofthreadsanddistributethetasksintothem(accordinglytoavailableCPUs)• Createamasterthreaddistributetheworkandcollecttheresults

Performances• Anextrathreadmeansmoresetup/teardowncosts• Implementmultithreadingonlyafterhavingprofiledyourapp• Lookfortaskswithasfewaspossibledependencies

ParallelAlgorithmsAlgorithmsthatconcurrentlyperformworkoncollectionsofdata

Fibonacci(n):1ifn<2:|threadA2returnn|threadA3x=spawnFibonacci(n-1)|threadA4y=spawnFibonacci(n-2)|threadB

WhyUsingMultithreading• Toimproveperformances(quickanddirtyanswer!)• ToavoidthattheGUIsuffersofthelatencyofeventdrivenarchitecture

Responsiveness• Keepheavytasksoutsidethemainthreadisthetraditionalwaytokeeptheappresponsive• Weactuallyloseoverallperformanceduetotheextracostofcontextswitching• Buttheuserexperienceisanywayimproved

Essentials• Multithreadingrequirecarefullyprogramming• Managethequeueofrequests• Coordinatingtheworkofmultiplethreads• Don'taccesstheUIfromanotUIthread• Trytoavoidcallingouttoexternalcodewhileholdingalock

MutualExclusion• Sometimeswewantthingstohappensimultaneously,andsometimesnot• Reasoningaboutconcurrentcomputationismostlyreasoningabouttime• Acquireandreleasesharedmembersinordertograntaccesstoonethreadpertime

ImplementingMutex• EachcriticalsectionisassociatedwithauniqueLockobject• Theaccesstothesectionislockedandunlockedwhentryingtoenterthecriticalsection

ConcurrentObjects• Whenmorethanonethreadworktogetherwearedealingwiththreadspools• Whenworkingwiththreadspoolseachthreadshouldbepausedandresumed• Managingconcurrencyit'spossibletoavoidblockingthreadsslowdowntheexectuintime

HandleRaceConditions• Iswhentwothreadsaccessthesamelocationinmemoryatthesametime(writing!)• Araceconditionisakindofbug,thathappensonlywithcertain

AvoidDeadlocksAdeadlockoccurswhenthewaitingprocessisstillholdingontoanotherresourcethatthefirstneedsbeforeitcanfinish

• Reducesthenumberofthreads(keepthingssimple!)• Don'tholdseverallocksatonce• Don'texecuteforeigncodewhileholdingalock

BeforeFlashPlayer11.3WhiletheunderlyingFlashPlayerandOperatingSystemusethreads,theexecutionmodelforActionscriptdoesnot

UIBlocking

if(appUI.isSlow){app.quality==='shittyapp'}

• Userinterfacenotresponsive• Theexpectedframeratedropssignificantly• Theuserexperiencewasdramaticallyimpacted

LocalConnectionTrick• LoadseparateSWFinthepage• HandlecommunicationthroughtheLocalConnectionclass

PseudoThreading• InvolvesENTER_FRAME,getTimer()andsomecalculation• NotalwaysreliableduetothegetTimer()reliability• Notsimpletomaintain

AsynchronousExecution

Workers,theHolyGrail!• CompleteseparateSWF• IndependentDisplayList

TheWorkerClass• AWorkerinstanceisavirtualinstanceoftheFlash/AIRruntime• EachWorkercontrolsandprovidesaccesstothelifecycleanddataofasingleworker• AworkerisjustanotherSWFthat’srunningalongsideyourmainSWF

WorkerSupport

if(Worker.isSupported){

}

• Desktop(FlashPlayer+AIR)• Mobile(AIRonAndroidonly,weird?!?)

CreateaWorkerWorkerDomain.current.createWorker(workerBytes);

TheworkerBytesvariablecontainstheByteArrayofanSWF,includeit:

• Usingthe[Embed]metatagtoembedthe.swffileintheapp

Isolation• Workersdonothaveaccesstothesamememory,variables,andcode• WorkersruninseparatedFlashPlayerinstances• Whatissharedbetweenworkersisunderyourcontrol

SharedProperties

setSharedProperty(key:String,value:*):void

• Eachworkercanexposeasetofpropertiestootherworkers• Eachworkercangetthevaluestoredinasharedproperty(fromanotherworker)• ValuesareNOTpassedbyreference(AMFserialization)

ReferencedDataTypes• Worker• MessageChannel

MessageChannel

while(mainToBack.messageAvailable){mainToBack.receive();}

• Sendone-waymessagesanddatafromoneworkertoanother• Treatsmessagesandrelatedvaluesasaqueue

SharedMemory• AShareableByteArray• Topassaroundareferencetoyoursharedmemory,youusetheMessageChannelAPI• WhenyoupassaByteArray,itisnotserialized,justreferenced• Youcanthenpassanythingyouwantbetweentheworkers(asrawbinarydata)

Mutex

varmutex:Mutex=newMutex();

• Locksaparticularblockofmemoryorsharedresource• AMutexmanagesaccessusingtheconceptofresourceownership

Condition

varcondition:Condition=Condition(mutex);

condition.wait(timeout:Number=-1);

• Suspendtheexecutionofaworker(i.e.thread)• Resumetheworkerexecutionunlockingtheassociatedmutex

Terminology• Promise,isanobjectwithathenmethod;itcanhave3states:pending,fulfilled,orrejected• Thenableisanobjectthatdefinesathenmethod• Reasonisavaluethatindicateswhyapromisewasrejected.

ApromiseApromiserepresentstheeventualresultofanasynchronousoperation

Thepointofpromisesistogiveusbackfunctionalcompositionanderrorbubblingintheasyncworld

ThenApromise'sthenmethodacceptstwoargumentsandreturnapromise

promise.then(onFulfilled,onRejected);

DeferredAdeferredobjectcandothesameasapromiseobject,butithastwofunctionstotriggerthedone()andfail()functions

PromisesvsDeferred• Thepromiseobjectdoesnothaveresolve()orreject()functions• Apromiseissomethingsharedw/otherobjects,whileadeferredshouldbekeptprivate

Resolvers• AreusedinternallybyDeferredstocreate,resolveandrejectPromises• Resolverscanpropagatefulfillmentandrejection

Q&A

ThanksGrazie!Graçias!Danke!Merci!��!