Principles of Software Construction: Objects, Design, and...

67
1 15-214 School of Computer Science Principles of Software Construction: Objects, Design, and Concurrency Concurrency: Motivation and Primitives Christian Kästner Bogdan Vasilescu

Transcript of Principles of Software Construction: Objects, Design, and...

Page 1: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

115-214

SchoolofComputerScience

PrinciplesofSoftwareConstruction:Objects,Design,andConcurrency

Concurrency:MotivationandPrimitives

ChristianKästner BogdanVasilescu

Page 2: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

215-214

Administrivia (1)• SignupprocedureanddeadlineforHW5a– Teamsof2or3.FormyourteamandsignupforapresentationtimebyThursday,Mar30,11:59pm.• Youmayutilizethe"SearchforTeammates"thread @5 tohelpyoufindteammates.

• Stickaroundafterclasstodayifyoudon’thavepartnersyet.• Twoplacestosignup:GoogleSheet&GitHubrepo.See@652

– Shortpresentation(max10min,6slidesorfewer)inrecitation onWednesday,April5 infrontofyourclassmates.• Goal:illustratehowyouachievereuseinadomain• Describedomain,examplesofplugins,decisionsregardinggeneralityvsspecificity,overallprojectstructure(e.g.,howarepluginsloaded),plugininterfaces

• Similartodesignreviewsessions

Page 3: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

315-214

Administrivia (2)

• Secondmidterm,ThursdayMar30inclass.• Midtermreviewsessiontoday 7:30pmin GHC4401.• Concurrencynottestedonthemidterm– Buteverythinginthecourseincludingreadingsisfairgame– Wewillfocusonthemiddlepartofthecourseandthethingsthatyouhadmorechancestopractice• e.g.moreUML/designthanAPIdesign

Page 4: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

415-214

Administrivia (3)

• GooddiscussiononPiazzaabouttheReadingQuizquestion2(Chapter6of“BeautifulCode”):

• Q:“Whataresomeofthementionedmechanismsthatcanhelpensurebackward-compatibility?”– A:Usingdesignpatterns– A:Usinginterfaces– A:Controllingvisibility

• A:Usingdesignpatterns:– Book:“Providewell-defined‘hookpoints’thatpermitextensibilityin

theplaceswhereyouintendittooccur.”– Exampleofhowthe Observerpatterncanbeusedtoprovidesuch

hookpoints

Page 5: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

515-214

Administrivia (4)

• Commitmessagesare(oneof)yourprimarymeansofcommunicationwiththerestoftheteam.– ThiswillbecomemoreobviousinHW5.

Page 6: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

615-214

Page 7: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

715-214

Part1:DesignataClassLevel

DesignforChange:InformationHiding,

Contracts,DesignPatterns,UnitTesting

DesignforReuse:Inheritance,Delegation,

Immutability,LSP,DesignPatterns

Part2:Designing(Sub)systems

UnderstandingtheProblem

ResponsibilityAssignment,DesignPatterns,GUIvsCore,

DesignCaseStudies

TestingSubsystems

DesignforReuseatScale:FrameworksandAPIs

Part3:DesigningConcurrent

Systems

ConcurrencyPrimitives,Synchronization

DesigningAbstractionsforConcurrency

DistributedSystemsinaNutshell

IntrotoJava

Git,CIStaticAnalysis

GUIsUML MoreGit

GUIsPerformance

Design

Page 8: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

815-214

LearningGoals

• Understandthemotivationanddifferentusecasesforconcurrencyandparallelism

• Understandconcurrencyrisks:safety,liveness,performance

• UnderstandanduseJavaprimitivesforconcurrency:threads,synchronization,volatile,wait/notify

Page 9: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

915-214

WHYCONCURRENCY

Page 10: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

1015-214

Whatisathread?(review)

• Shortforthreadofexecution• Multiplethreadsruninsameprogramconcurrently

• Threadssharethesameaddressspace– Changesmadebyonethreadmaybereadbyothers

• Multithreadedprogramming– Alsoknownasshared-memorymultiprocessing

Page 11: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

1115-214

Processorcharacteristicsovertime

Page 12: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

1215-214

PowerrequirementsofaCPU

• power=capacitance× voltage2 × frequency• Toincreaseperformance– Moretransistors,thinnerwires

• Morepowerleakage:increasevoltage– Increaseclockfrequency

• Changeelectricalstatefaster:increasevoltage

• Dennardscaling– astransistorsgetsmaller,powerdensityisapproximatelyconstant…– …untilearly2000s

• Now:Powerissuper-linearinCPUperformance

Page 13: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

1315-214

FailureofDennard Scalingforcedourhand

• Mustreduceheatbylimitingpower• Limitpowerbyreducingfrequencyand/orvoltage• Inotherwords,buildslowercores…– …butbuildmoreofthem

• Addingcoresupspowerlinearlywithperformance

• Butconcurrencyisrequiredtoutilizemultiplecores

Page 14: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

1415-214

Concurrencythenandnow

• Inpastmulti-threadingjustaconvenientabstraction– GUIdesign:eventdispatchthread– Serverdesign:isolateeachclient’swork–Workflowdesign:isolateproducersandconsumers

• Now:required forscalabilityandperformance

Page 15: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

1515-214

BenefitsofThreads(1)• ExploitingMultipleProcessors– AllCPUstodayaremulti-core– Butbasicunitofschedulingisathread

• Asingle-threadedprogramrunningona100-processorsystemisgivingupaccessto99%oftheavailableCPUresources

– Also,betterthroughputonsingle-processorsystems:• Single-threadedprogramneedstowaitforsynchronousI/Ooperationtocomplete

• Multi-threadedprogramcandosomethingelseduringtheblockingI/O

• ResponsiveUserInterfaces– AWT,Springhaveseparateeventdispatchthread– Long-runningtasks(e.g.,spellchecking)canbeexecutedinseparatethread

Page 16: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

1615-214

BenefitsofThreads(2)

• SimplicityofModeling– Separatingtasks&assigningaseparatethreadtoeach

– Abstractingcommoninfrastructure,asrequestmanagement,loadbalancing,...inconcurrencyframeworks

• SimplifiedHandlingofAsynchronousEvents– Async vssyncI/O:serverthatacceptssocketconnectionsfrommultipleclients;clientreadblocksuntildataisavailable

– Avoiding“callbackhell”(JavaScript)

Page 17: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

1715-214

Aside:JavaScript“CallbackHell”• Youdon’twanttheprogramtopause(block)whilewaitingfordownloadtofinish

• Storethecodethatshouldrunafterthedownloadiscompleteina“callback”function

var photo = downloadPhoto('http://coolcats.com/cat.gif')// photo is 'undefined'!

downloadPhoto('http://coolcats.com/cat.gif', handlePhoto)

function handlePhoto (error, photo) {if (error) console.error('Download error!', error)else console.log('Download finished', photo)

}

console.log('Download started')

From: http://callbackhell.com

1

2

3

4

Page 18: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

1815-214

Aside:JavaScript“CallbackHell”• Callbackscangetoutofhand

getData(function(a){ getMoreData(a, function(b){

getMoreData(b, function(c){ getMoreData(c, function(d){

getMoreData(d, function(e){ ...

});});

});});

});

From: http://stackabuse.com/avoiding-callback-hell-in-node-js/

Page 19: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

1915-214

Weareallconcurrentprogrammers

• Javaisinherentlymultithreaded• Inordertoutilizeourmulticoreprocessors,wemustwritemultithreadedcode

• Goodnews:alotofitiswrittenforyou– Excellentlibrariesexist(java.util.concurrent)

• Badnews:youstillmustunderstandfundamentals– touselibrarieseffectively– todebugprogramsthatmakeuseofthem

Page 20: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

2015-214

ConcurrencyvsParallelism

Page 21: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

2115-214

CONCURRENCYHAZARDSSafety,Liveness,Performance

Page 22: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

2215-214

SafetyHazard• Theorderingofoperationsinmultiplethreadsisunpredictable.

• UnluckyexecutionofUnsafeSequence.getNext

@NotThreadSafepublic class UnsafeSequence {

private int value;

public int getNext() {return value++;

}}

valueà9 9+1à10 valueà10

valueà9 9+1à10 valueà10

A

B

Not atomic

Page 23: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

2315-214

ThreadSafety

Aclassisthreadsafeifitbehavescorrectlywhenaccessedfrommultiplethreads,regardlessoftheschedulingorinterleavingoftheexecutionofthosethreadsbytheruntimeenvironment,andwithnoadditionalsynchronizationorothercoordinationonthepartofthecallingcode.

Page 24: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

2415-214

LivenessHazard• Safety:“nothingbadeverhappens”• Liveness:“somethinggoodeventuallyhappens”

• Deadlock– Infiniteloopinsequentialprograms– ThreadAwaitsforaresourcethatthreadBholdsexclusively,andBneverreleasesità Awillwaitforever• E.g.,Diningphilosophers

• Elusive:dependonrelativetimingofeventsindifferentthreads

Page 25: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

2515-214

Deadlockexample

class Account {double balance;

void withdraw(double amount){ balance -= amount; }

void deposit(double amount){ balance += amount; }

void transfer(Account from, Account to, double amount){synchronized(from) {

from.withdraw(amount);synchronized(to) {

to.deposit(amount);}

}}

}

• Twothreads:Adoestransfer(a,b,10); Bdoestransfer(b,a,10)

Execution trace:A: lock a (v)B: lock b (v)A: lock b (x)B: lock a (x)A: waitB: wait

Deadlock!

Page 26: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

2615-214

PerformanceHazard• Liveness:“somethinggoodeventuallyhappens”• Performance:wewantsomethinggoodtohappenquickly

• Multi-threadinginvolvesruntimeoverhead:– Coordinatingbetweenthreads(locking,signaling,memorysync)– Contextswitches– Threadcreation&teardown– Scheduling

• Notallproblemscanbesolvedfasterwithmoreresources– Onemotherdeliversababyin9months

Page 27: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

2715-214

Amdahl’slaw• Thespeedupis

limitedbytheserialpartoftheprogram.

Page 28: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

2815-214

Howfastcanthisrun?

public class WorkerThread extends Thread {...

public void run() {while (true) {

try {Runnable task = queue.take();task.run();

} catch (InterruptedException e) {break; /* Allow thread to exit */

}}

}}

• Nthreadsfetchindependenttasksfromasharedworkqueue

Page 29: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

2915-214

JAVAPRIMITIVES:ENSURINGVISIBILITYANDATOMICITY

Page 30: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

3015-214

SynchronizationforSafety

• Ifmultiplethreadsaccessthesamemutablestatevariablewithoutappropriatesynchronization,theprogramisbroken.

• Therearethreewaystofixit:– Don'tsharethestatevariableacrossthreads;–Makethestatevariableimmutable;or– Usesynchronizationwheneveraccessingthestatevariable.

Page 31: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

3115-214

Exclusion

Synchronizationallowsparallelismwhileensuringthatcertainsegmentsareexecutedinisolation.Threadswaittoacquirelock,mayreduceperformance.

Page 32: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

3215-214

Statelessobjectsarealwaysthreadsafe

• Example:statelessfactorizer– Nofields– Noreferencestofieldsfromotherclasses– Threadssharingitcannotinfluenceeachother

@ThreadSafepublic class StatelessFactorizer implements Servlet {

public void service(ServletRequest req, ServletResponse resp) {BigInteger i = extractFromRequest(req);BigInteger[] factors = factor(i);encodeIntoResponse(resp, factors);

}}

Page 33: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

3315-214

Isthisthreadsafe?

public class CountingFactorizer implements Servlet {private long count = 0;

public long getCount() { return count; }

public void service(ServletRequest req, ServletResponse resp) {BigInteger i = extractFromRequest(req);BigInteger[] factors = factor(i);++count;encodeIntoResponse(resp, factors);

}}

Page 34: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

3415-214

Nonatomicityandthread(un)safety

@NotThreadSafepublic class UnsafeCountingFactorizer implements Servlet {

private long count = 0;

public long getCount() { return count; }

public void service(ServletRequest req, ServletResponse resp) {BigInteger i = extractFromRequest(req);BigInteger[] factors = factor(i);++count;encodeIntoResponse(resp, factors);

}}

valueà9 9+1à10 valueà10

valueà9 9+1à10 valueà10

A

B

Page 35: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

3515-214

Nonatomicityandthread(un)safety

• Stateful factorizer– Susceptibletolostupdates– The++count operationisnotatomic(read-modify-write)

@NotThreadSafepublic class UnsafeCountingFactorizer implements Servlet {

private long count = 0;

public long getCount() { return count; }

public void service(ServletRequest req, ServletResponse resp) {BigInteger i = extractFromRequest(req);BigInteger[] factors = factor(i);++count;encodeIntoResponse(resp, factors);

}}

Page 36: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

3615-214

Enforcingatomicity:Intrinsiclocks

• synchronized(lock) { … } synchronizesentirecodeblockonobjectlock;cannotforgettounlock

• Thesynchronizedmodifieronamethodisequivalenttosynchronized(this) { … }aroundtheentiremethodbody

• EveryJavaobjectcanserveasalock• Atmostonethreadmayownthelock(mutualexclusion)– synchronized blocksguardedbythesamelockexecuteatomicallyw.r.t.oneanother

Page 37: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

3715-214

Fixingthestateful factorizer@ThreadSafepublic class UnsafeCountingFactorizer

implements Servlet {@GuardedBy(“this”)private long count = 0;

public long getCount() { synchronized(this){

return count; }

}

public void service(ServletRequest req, ServletResponse resp) {

BigInteger i = extractFromRequest(req);BigInteger[] factors = factor(i);synchronized(this) {

++count;}encodeIntoResponse(resp, factors);

}}

Foreachmutablestatevariablethatmaybeaccessedbymorethanonethread,all accessestothatvariablemustbeperformedwiththesame lockheld.Inthiscase,wesaythatthevariableisguardedbythatlock.

Page 38: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

3815-214

Fixingthestateful factorizer@ThreadSafepublic class UnsafeCountingFactorizer

implements Servlet {@GuardedBy(“this”)private long count = 0;

public synchronized long getCount() { return count;

}

public void service(ServletRequest req, ServletResponse resp) {

BigInteger i = extractFromRequest(req);BigInteger[] factors = factor(i);synchronized(this) {

++count;}encodeIntoResponse(resp, factors);

}}

Foreachmutablestatevariablethatmaybeaccessedbymorethanonethread,all accessestothatvariablemustbeperformedwiththesame lockheld.Inthiscase,wesaythatthevariableisguardedbythatlock.

Page 39: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

3915-214

Fixingthestateful factorizer@ThreadSafepublic class UnsafeCountingFactorizer

implements Servlet {@GuardedBy(“this”)private long count = 0;

public synchronized long getCount() { return count;

}

public synchronized void service(ServletRequest req, ServletResponse resp) {

BigInteger i = extractFromRequest(req);BigInteger[] factors = factor(i);

++count;encodeIntoResponse(resp, factors);

}}

Foreachmutablestatevariablethatmaybeaccessedbymorethanonethread,all accessestothatvariablemustbeperformedwiththesame lockheld.Inthiscase,wesaythatthevariableisguardedbythatlock.

Page 40: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

4015-214

What’sthedifference?

public synchronized void service(ServletRequest req, ServletResponse resp) {

BigInteger i = extractFromRequest(req);BigInteger[] factors = factor(i);

++count;encodeIntoResponse(resp, factors);

}

public void service(ServletRequest req, ServletResponse resp) {

BigInteger i = extractFromRequest(req);BigInteger[] factors = factor(i);synchronized(this) {

++count;}encodeIntoResponse(resp, factors);

}

Page 41: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

4115-214

Privatelocks@ThreadSafepublic class UnsafeCountingFactorizer

implements Servlet {private final Object lock = new Object();@GuardedBy(“lock”)private long count = 0;

public long getCount() { synchronized(lock){

return count; }

}

public void service(ServletRequest req, ServletResponse resp) {

BigInteger i = extractFromRequest(req);BigInteger[] factors = factor(i);synchronized(lock) {

++count;}encodeIntoResponse(resp, factors);

}}

Foreachmutablestatevariablethatmaybeaccessedbymorethanonethread,all accessestothatvariablemustbeperformedwiththesame lockheld.Inthiscase,wesaythatthevariableisguardedbythatlock.

Page 42: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

4215-214

Doesthisdeadlock?

public class Widget {public synchronized void doSomething() {...}

}

public class LoggingWidget extends Widget {public synchronized void doSomething() {

System.out.println(toString() + ": calling doSomething");super.doSomething();

}}

Page 43: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

4315-214

No:Intrinsiclocksarereentrant• Athreadcanlockthesameobjectagainwhilealreadyholdingalock,i.e.,asynchronizedmethodcancallanothersynchronizedmethodinthesameobject

public class Widget {public synchronized void doSomething() {...}

}

public class LoggingWidget extends Widget {public synchronized void doSomething() {

System.out.println(toString() + ": calling doSomething");super.doSomething();

}}

Page 44: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

4415-214

CooperativethreadterminationHowlongwouldyouexpectthistorun?

public class StopThread {private static boolean stopRequested;

public static void main(String[] args) throws Exception {Thread backgroundThread = new Thread(() -> {

while (!stopRequested)/* Do something */ ;

});backgroundThread.start();

TimeUnit.SECONDS.sleep(5);stopRequested = true;

}}

Page 45: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

4515-214

Whatcouldhavegonewrong?

• Intheabsenceofsynchronization,thereisnoguaranteeastowhen,ifever,onethreadwillseechangesmadebyanother!

• VMscananddoperformthisoptimization:while (!done)

/* do something */ ;

becomes:if (!done)

while (true)/* do something */ ;

Page 46: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

4615-214

Howdoyoufixit?public class StopThread {

@GuardedBy(“StopThread.class”)private static boolean stopRequested;

private static synchronized void requestStop() {stopRequested = true;

}

private static synchronized boolean stopRequested() {return stopRequested;

}

public static void main(String[] args) throws Exception {Thread backgroundThread = new Thread(() -> {

while (!stopRequested())/* Do something */ ;

});backgroundThread.start();

TimeUnit.SECONDS.sleep(5);requestStop();

}}

Page 47: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

4715-214

Youcandobetter(?)volatile issynchronizationsansmutualexclusionpublic class StopThread {

private static volatile boolean stopRequested;

public static void main(String[] args) throws Exception {Thread backgroundThread = new Thread(() -> {

while (!stopRequested)/* Do something */ ;

});backgroundThread.start();

TimeUnit.SECONDS.sleep(1);stopRequested = true;

}}

Page 48: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

4815-214

Volatilekeyword

• Tellscompilerandruntimethatvariableissharedandoperationsonitshouldnotbereorderedwithothermemoryops– Areadofavolatilevariablealwaysreturnsthemostrecentwritebyanythread

• Volatileisnotasubstituteforsynchronization– Volatilevariablescanonlyguaranteevisibility– Lockingcanguaranteebothvisibilityandatomicity

Page 49: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

4915-214

Summary:Synchronization

• Ideally,avoidsharedmutablestate

• Ifyoucan’tavoidit,synchronizeproperly– Failuretodosocausessafetyandliveness failures– Ifyoudon’tsyncproperly,yourprogramwon’twork

• Evenatomicoperationsrequiresynchronization– e.g.,stopRequested = true

– Andsomethingsthatlookatomicaren’t(e.g.,val++)

Page 50: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

5015-214

JAVAPRIMITIVES:WAIT,NOTIFY,ANDTERMINATION

Page 51: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

5115-214

Guardedmethods

• Whattodoonamethodifthepreconditionisnotfulfilled(e.g.,transfermoneyfrombankaccountwithinsufficientfunds)• throwexception(balking)• waituntilpreconditionisfulfilled(guarded suspension)• waitandtimeout(combinationofbalkingandguarded

suspension)

Page 52: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

5215-214

Guardedsuspension

• Blockexecutionuntilagivenconditionistrue• Forexample,– pullelementfromqueue,butwaitonanemptyqueue

– transfermoneyfrombankaccountassoonsufficientfundsarethere

• Blockingas(oftensimpler)alternativetocallback

Page 53: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

5315-214

MonitorMechanicsinJava

• Object.wait()– suspendsthecurrentthread’sexecution,releasinglocks

• Object.wait(timeout)– suspendsthecurrentthread’sexecutionforuptotimeoutmilliseconds

• Object.notify()– resumesoneofthewaitingthreads

• Seedocumentationforexactsemantics

Page 54: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

5415-214

MonitorExampleclass SimpleBoundedCounter {

protected long count = MIN;public synchronized long count() { return count; }public synchronized void inc() throws InterruptedException {

awaitUnderMax(); setCount(count + 1);}public synchronized void dec() throws InterruptedException {

awaitOverMin(); setCount(count - 1);}protected void setCount(long newValue) { // PRE: lock held

count = newValue;notifyAll(); // wake up any thread depending on new value

}protected void awaitUnderMax() throws InterruptedException {

while (count == MAX) wait();}protected void awaitOverMin() throws InterruptedException {

while (count == MIN) wait();}

}

Page 55: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

5515-214

Never invokewaitoutsidealoop!

• Looptestsconditionbeforeandafterwaiting• Testbeforeskipswait ifconditionalreadyholds– Necessarytoensureliveness–Withoutit,threadcanwaitforever!

• Testingafterwait ensuresafety– Conditionmaynotbetruewhenthreadwakens– Ifthreadproceedswithaction,itcandestroyinvariants!

Page 56: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

5615-214

All ofyourwaitsshouldlooklikethissynchronized (obj) {

while (<condition does not hold>) {obj.wait();

}

... // Perform action appropriate to condition}

Page 57: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

5715-214

Whycanathreadwakefromawaitwhenconditiondoesnothold?

• Anotherthreadcanslipinbetweennotify&wake• Anotherthreadcaninvokenotify accidentallyormaliciouslywhenconditiondoesnothold– Thisisaflawinjavalockingdesign!– Canworkaroundflawbyusingprivatelockobject

• Notifier canbeliberalinwakingthreads– UsingnotifyAll isgoodpractice,butcausesthis

• Waitingthreadcanwakeupwithoutanotify(!)– Knownasaspuriouswakeup

Page 58: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

5815-214

Interruption

• Difficulttokillthreadsoncestarted,butmaypolitelyasktostop(thread.interrupt())

• Long-runningthreadsshouldregularlycheckwhethertheyhavebeeninterrupted

• Threadswaitingwithwait()throwexceptionsifinterrupted

• Readdocumentation

public class Thread {public void interrupt() { ... }public boolean isInterrupted() { ... }...

}

Page 59: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

5915-214

InterruptionExample

For details, see Java Concurrency In Practice, Chapter 7

class PrimeProducer extends Thread {private final BlockingQueue<BigInteger> queue;PrimeProducer(BlockingQueue<BigInteger> queue) {

this.queue = queue;}public void run() {

try {BigInteger p = BigInteger.ONE;while (!Thread.currentThread().isInterrupted())

queue.put(p = p.nextProbablePrime());} catch (InterruptedException consumed) {

/* Allow thread to exit */}

}public void cancel() { interrupt(); }

}

Page 60: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

6015-214

BUILDINGHIGHERLEVELCONCURRENCYMECHANISMS

Page 61: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

6115-214

BeyondJavaPrimitives

• JavaPrimitives(synchronized,wait,notify)arelowlevelmechanisms

• Formosttasksbetterhigher-levelabstractionsexist

• Writingownabstractionsispossible,butpotentiallydangerous– uselibrarieswrittenbyexperts

Page 62: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

6215-214

Example:read-writelocks(API)Alsoknownasshared/exclusivemodelocksprivate final RwLock lock = new RwLock();

lock.readLock();try {

// Do stuff that requires read (shared) lock} finally {

lock.unlock();}

lock.writeLock();try {

// Do stuff that requires write (exclusive) lock} finally {

lock.unlock();}

Page 63: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

6315-214

Example:read-writelocks(Impl.1/2)

public class RwLock {// State fields are protected by RwLock's intrinsic lock

/** Num threads holding lock for read. */private int numReaders = 0;

/** Whether lock is held for write. */private boolean writeLocked = false;

public synchronized void readLock() throws InterruptedException { while (writeLocked) {

wait();}numReaders++;

}

Page 64: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

6415-214

Example:read-writelocks(Impl.2/2)

public synchronized void writeLock() throws InterruptedException {while (numReaders != 0 || writeLocked) {

wait();}writeLocked = true;

}

public synchronized void unlock() {if (numReaders > 0) {

numReaders--;} else if (writeLocked) {

writeLocked = false;} else {

throw new IllegalStateException("Lock not held");}notifyAll(); // Wake any waiters

}}

Page 65: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

6515-214

Caveat:RwLock isjustatoy!

• Ithaspoorfairnessproperties– Readerscanstarvewriters!

• java.util.concurrent providesanindustrialstrengthReadWriteLock

• Moregenerally,avoidwait/notify– Intheearlydaysitwasallyouhad– Nowadays,higherlevelconcurrencyutils arebetter

Page 66: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

6615-214

Summary

• Concurrencyforexploitingmultipleprocessors,simplifyingmodeling,simplifyingasynchronousevents

• Safety,livenessandperformancehazardsmatter• SynchronizationonanyJavaobject;volatileensuresvisibility

• Wait/notifyforguards,interruptionforcancelation– buildingblocksforhigherlevelabstractions

Page 67: Principles of Software Construction: Objects, Design, and ...ckaestne/15214/s2017/slides/20170328-concurrency-1.pdf15-214 3 Administrivia(2) • Second midterm, Thursday Mar 30 in

6715-214

RecommendedReadings

• Goetzetal.JavaConcurrencyInPractice.PearsonEducation,2006,Chapters1-2

• Lea,Douglas.ConcurrentprogramminginJava:designprinciplesandpatterns.Addison-WesleyProfessional,2000.