Cocurrecy Threads ad Cocurrecy i Java: Part 1 What every computer egieer eeds to kow about cocurrecy: Cocurrecy is to utraied programmers as matches are to small childre. It is all too easy to get bured. 1 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 2 Cocurrecy: What Cocurrecy: Where Cocurrecy meas that there are multiple agets ruig at the same time ad iteractig. Sources of cocurrecy: We have cocurrecy whe we have iteractig processes ruig o differet computers (e.g. Apache a web server o moa.egr.mu.ca ad Firefox a web browser o paradox.egr.mu.ca ) 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 3 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 4
Cocurrecy : Where Cocurrecy : Where We also have cocurrecy whe we have iteractig processes ruig o the same computer. E.g. Firefox ad Widows Explorer. Every iteractive program is part of a cocurret system: the user is a cocurret aget. Furthermore we ca have multiple threads of cotrol withi oe OS process. A.K.A. multithreadig Cocurrecy ca be itermachie, iterprocess, or multithreadig. These slides cocetrate o itraprocess cocurrecy i Java. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 5 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 6 Cocurrecy: Why Cocurrecy: Why Reasos for usig cocurrecy Speed. Multiple threads ca be ru o multiple processors (or multiple cores). This may give a speed advatage. Distributio. We may wish differet parts of a system to be located o differet machies for reasos of coveiece, security, reliability,. Reasos for usig cocurrecy Asychroy. It is easiest to deal with multiple sources of evets by havig oe thread dedicated to each stream of icomig or outgoig evets. For example, a web browser may use oe thread to deal with iput from the user ad oe thread for each server it is curretly iteractig with. Likewise a web server will typically dedicate at least oe thread to each curret sessio. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 7 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 8
Threads Each thread has its ow program couter registers local variables ad stack All threads share the same heap (objects) Cocurrecy: How Multiple processors Multiprocessor (ad mulitcore) CPU L1 Cache CPU L1 Cache 2 threads 2 processors time L2 Cache Cetral Memory 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 9 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 10 Cocurrecy: How Time slicig implemetatio 2 threads 1 processor Cotext switch Memory Previously The This Pipelie PC Iitially ad gree is a registers the coceptual saved PC drais thread red thread ow are view ad saved. executes register values are is of executig the process. fetched. Implemetatio details will vary. Sigle processor The CPU is switched betwee threads at upredictable times time Copy of PC Copy of registers PC Registers Copy of PC Copy of registers Multiple processor Thread may occasioally migrate 3 threads 2 processors Stack IF DEC EX WB CPU Stack time Istructios Heap 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 11 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 12
Thread Objects i Java I Java, threads are represeted by objects of class java.lag.thread. The ru method cotais the actual code for the thread to execute. public class ThreadExample exteds Thread { private Strig message; ThreadExample( Strig message ) { this.message = getname() + ": " + message; @Override public void ru() { for( it i=0 ; i<20 ; ++i ) System.out.pritl( message ); 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 13 Startig a ew thread Callig t.start() starts a ew thread which executes the t.ru() public class ThreadExampleMai { public static void mai(strig[] args) { ThreadExample thread0 = ew ThreadExample("Hi"); ThreadExample thread1 = ew ThreadExample("Ho"); thread0.start(); thread1.start(); System.out.pritl( Thread.curretThread().getName() + ": Mai is doe"); 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 14 Output for example Race Coditios Possible output for example: Thread-1: Ho Thread-1: Ho Thread-0: Hi mai: Mai is doe Thread-1: Ho Thread-0: Hi (ad so o for aother 35 lies) Whe t.ru() completes the thread stops. Whe all threads have stopped the program exits A system has a race coditio whe its correctess depeds the order of certai evets, but the order of those evets is ot sufficietly cotrolled by the desig of the system. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 15 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 16
Race Coditios Race Coditios Ofte race coditios occur because 2 agets have ucotrolled access to a shared resource Cosider two trai routes that share the same bridge A solutio: Before crossig the bridge, trais acuire a toke After crossig the bridge, trais reliuish the toke Acuire the toke Reliuish the toke Uless access to the shared resource is cotrolled, disaster may esue. Acuire the toke Reliuish the toke Oly Both As Iitially the soo oe trais other as ca try trai toke the succeed. toke ca acuire is free acuire is free at the the agai same toketime 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 17 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 18 Race Coditios i Software Remember: objects are shared by threads I Java, access to a object s methods is ucotrolled, by default!!!! Suppose we have public class Couter { private it cout = 0; public void icremet() { ++cout; System.out.pritl(cout); Race Coditios i Software Differet threads ca share the same Couter public class CouterThread exteds Thread { private Couter couter; CouterThread(Couter c) { this.couter = c; @Override public void ru() { for( it i=0 ; i<10 ; ++i ) { couter.icremet(); 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 19 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 20
Race Coditios i Software Execute the followig: public class CouterMai { public static void mai(strig[] args) { Couter c = ew Couter(); // Threads p ad share the same couter CouterThread p = ew CouterThread(c); CouterThread = ew CouterThread(c); p.start();.start(); Race Coditios i Software public class CouterMai { public static void mai(strig[] args) { Couter c = ew Couter(); CouterThread p = ew CouterThread(c); CouterThread = ew CouterThread(c); p.start();.start(); Possible Result: 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 WTF? 11 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 21 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 22 Race Coditios i Software The reaso is that the icremet operatio results i multiple bytecode (low-level JVM) istructios that ca get iterleaved Focus oly o ++cout public class Couter { private it cout = 0; Race Coditios i Software The statemet ++cout results i the followig bytecode load cout to r0 store r0 to cout public void icremet() { ++cout; System.out.pritl(cout); r0 represets register 0 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 23 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 24
Race Coditios i Software Two threads ivoke icremet at about the same time (Recall: Registers are local to the thread.) A race coditio results. p cout r0 (i p) r0 (i ) load cout to r0 store r0 to cout load cout to r0 store r0 to cout 41 42 42 41+1+1 = 42? Of the two icremets, oe was lost. 41 42 41 42 Race Coditios: Aother Example Cosider trasfers o a accout class AccoutMaager { private Accout savigs ; private Accout cheuig; public void trasfertosavigs( it amout ) { it s = savigs.getbalace() ; it c = checkig.getbalace() ; savigs.setbal( s+amout ) ; cheuig.setbal( c-amout ) ; Two threads execute trasfers. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 25 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 26 Race Coditios : Aother Example Oe Thread Aother Thread sav ch (amout = 500) (amout = 1000) s = savigs.getbalace() 3000 3000 c = cheuig.getbalace() s = savigs.getbalace() 4000 savigs.setbal(s+1000) 2000 cheuig.setbal(c-1000) c = cheuig.getbalace() 2000 3500 savigs.setbal(s+500) 1500 cheuig.setbal(c-500) sychroized to the rescue Methods may be declared sychroized public class Couter { private it cout = 0; public sychroized void icremet() { ++cout; System.out.pritl(cout); I started with $6000 ad eded with $5000. This is ot good. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 27 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 28
sychroized to the rescue sychroized to the rescue Each object has a associated toke called its lock. Whe a thread ivokes a sychroized method x.m(): At each poit i time, each lock either is owed by o thread or is owed by oe thread. If it does ot already ow the recipiet s (x s) lock, It waits util it ca acuire the lock A thread that attempts to acuire a lock must wait util o thread ows the lock. After acuirig a lock, a thread ows it util it reliuishes it. Oce the lock has bee acuired the thread begis to execute the method Whe a thread leaves a ivocatio of a sychroized method: If it is leavig the last sychoroized ivocatio for that object It reliuishes the lock as it leaves 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 29 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 30 sychroized to the rescue sychroized to the rescue Hece, for ay object x, at most oe thread may be executig ay of x s sychroized methods. Example: Two threads ivoke c.icremet() at about the same time. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 31 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 32
sychroized to the rescue Thread p reuest lock o object c acuire lock o object c load cout to r0 store r0 to cout reliuish lock o object o Thread reuest lock o object o waits waits waits waits acuire lock o object o load cout to r0 store r0 to cout reliuish lock o object o 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 33 The Room metaphor Whe a thread Each has left all object is sychroized a room methods of the object, it leaves At the all room times, at most oe allowig thread is aother allowed thread to eter. to be i the room. The atechamber The room Whe a thread ot i the room calls a sychroized method, it eters a atechamber. There it waits util the room is empty. Whe the room is empty, at most oe thread is allowed to eter the room 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 34 Desig rule: Shared Objects Accouts For ay object that might be used by more tha oe thread at the same time Declare all methods that access or mutate the data sychroized (Note: private methods are exempted from this rule, as they ca oly be called oce a oprivate method has bee called.) Costructors eed ot (ad ca ot) be sychroized I AccoutMaager add sychroized to all methods class AccoutMaager { private Accout savigs ; private Accout cheuig; public sychroized void trasfertosavigs( it amout ) { it s = savigs.getbalace() ; it c = checkig.getbalace() ; savigs.setbal( s+amout ) ; cheuig.setbal( c-amout ) ; 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 35 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 36