Threads ad Cocurrecy i Java: Part 2 1
Waitig Sychroized methods itroduce oe kid of coordiatio betwee threads. Sometimes we eed a thread to wait util a specific coditio has arise. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 2
Waitig by Pollig For example i a Couter class we might wat to wait util the cout is 0 or less. We could do this class Couter { private it cout ; Note that waitforzeroorless is ot sychroized public Couter( it cout ) { this.cout = cout ; sychroized void decremet() { cout -= 1; sychroized itgetcout() { retur cout ; void waitforzeroorless() { // Pollig loop while( getcout() > 0 ) /*do othig*/; 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 3
Waitig by Pollig Slightly improvig this we ca write void waitforzeroorless() { // Pollig loop while( getcout() > 0 ) Thread.yield(); which keeps the thread from hoggig the CPU while pollig. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 4
Waitig by wait ad otifyall Better, we ca have a thread wait util it is otified that the coditio has (or may have) come about. While waitig, the thread reliuishes owership ad waits util some later time. To wait, a thread seds the object a wait message. To allow other threads to stop waitig, threads seds a otifyall message to the object. wait ad otifyall are methods of class Object. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 5
The Room metaphor Whe a thread i the room seds a wait message, it leaves the room ad eters the waitig room. Later aother thread eters. Add a waitig room Waitig room The atechamber The T1 room 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 6 T2 It seds a otifyall message. All threads i the waitig room move to the atechamber. Ad so they ca reeter whe the room is empty.
wait ad otifyall example. class Couter { private it cout ; Note that waitforzeroorless is sychroized public Couter( it cout ) { this.cout = cout ; sychroized void decremet() { cout -= 1 ; if( cout <= 0 ) otifyall() ; sychroized void waitforzero() { while( cout > 0 ) { try { wait(); catch( IterruptedExceptio e ) { 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 7
A possible seuece Oe thread Calls waitforzero eters the room Sees cout is 1. calls wait, eters the waitig room waits i waitig room waits i atechamber reeters the room returs from wait sees cout is 0 ad returs Aother thread Calls decremet eters the room. cout-- call otifyal,l movig other thread to wait ueue for the lock returs leavig the room 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 8
Passig messages We wat to pass messages betwee ay umber of producers ad cosumers executig o ay umber of threads. producer producer producer cosumer cosumer sed(0) receive() :Mailbox 0 { 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 9
Passig messages We wat to pass messages betwee threads so that Each message is received o more tha oce No message is overwritte before it is received. A receiver may have to wait util a ew message is set A seder may have to wait util there is room i the ueue Up to 100 messages ca be set but ot yet received. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 10
Passig messages Receivers may eed to wait. Suppose the mailbox iitially has o messages producer producer producer cosumer cosumer sed(1) receive() :Mailbox 1 { 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 11
Passig messages Producers may eed to wait. Suppose the capacity is 5 ad the mailbox is full producer producer producer cosumer cosumer sed(6) receive() :Mailbox 1 { 2 3 4 56 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 12
Passig messages icomplete class Mailbox<MessageType> { private static fial it CAP = 100 ; private Queue<MessageType> = ew Queue<MessageType>() ; // ivariat:.size() <= CAP public sychroized void sed(messagetype mess) { // wait util.size()< CAP.put( mess ); To do public sychroized MessageType receive() { // wait util.size() > 0 retur.take(); To do 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 13
Passig messages add waits class Mailbox<MessageType> { private static fial it CAP = 100 ; private Queue<MessageType> = ew Queue<MessageType>() ; // ivariat:.size() <= CAP public sychroized void sed(messagetype mess) { // wait util.size()< CAP while(.put( mess.size()==cap ) ; ) { try { wait() ; catch( IterruptedExceptio e ) { public sychroized MessageType receive() { // wait util.size() > 0 while( retur.size()==0.take() ) { try { wait() ; catch( IterruptedExceptio e ) { To do To do 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 14
Passig messages add otificatios class Mailbox<MessageType> { private static fial it CAP = 100 ; private Queue<MessageType> = ew Queue<MessageType>() ; // ivariat:.size() <= CAP public sychroized void sed(messagetype mess) { // wait util.size()< CAP while(.size()==cap ) { try { wait() ; catch( IterruptedExceptio e ) {.put( mess ); otifyall() ; public sychroized MessageType receive() { // wait util.size() > 0 while(.size()==0 ) { try { wait() ; catch( IterruptedExceptio e ) { retur otifyall();.take(); Doe
Eve better tha wait ad otifyall As software gets more complex, usig wait ad otifyall ca be a bit awkward ad is easy to mess up. A improvemet is to use Dr. Norvell s moitor package. See http://www.egr.mu.ca/~theo/misc/moitors/moitors.html 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 16
Deadlock While waitig ca solve safety issues (e.g. esurig ocorruptio of data, oduplicatio of messages, oloss of messages), it ca cause liveess problems. I particular if oe thread is waitig for aother to do somethig, ad the other thread is waitig for the first to do somethig, the we have deadlock 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 17
Deadlock Example Suppose we have a bak accout class class Accout { private it balace = 0.0; public sychroized void addfuds( it amout ) { balace += amout; public void trasfer ( it amout, Accout toaccout ) { if( balace >= amout ) { toaccout.addfuds( amout ); balace -= amout; else { 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 18
Deadlock Example There is a subtle problem here. The itet is that oe should ot be able to trasfer out of a accout more moey tha it has. (A safety problem.) But, if two threads attempt to trasfer from the same accout at about the same time, the they might both succeed, eve though the fial balace will be egative. To fix this, we make trasfer sychroized. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 19
Deadlock Example class Accout { private it balace = 0; public sychroized void addfuds( it amout ) { balace += amout; public sychroized void trasfer (it amout, Accout toaccout){ if( balace >= amout ) { toaccout.addfuds( amout ); balace -= amout; else { 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 20
Deadlock Example But ow deadlock is possible. Suppose thread 0 tries to trasfer from accout x to accout y. At roughly the same time thread 1 attempts to trasfer from accout y to accout x 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 21
A possible seuece Thread 0 calls x.trasfer(100, y) obtais a lock o x calls y.addfuds() waits for lock o y waits for lock o y waits for lock o y waits for lock o y ad ifiitum Thread 1 calls y.trasfer( 50, x ) obtais a lock o y calls x.addfuds() waits for lock o x waits for lock o x waits for lock o x waits for lock o x ad ifiitum The Threads are ow deadlocked! 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 22
A solutio to deadlock Oe solutio is to always lock objects i a particular order. e.g. give each lockable object a globally uiue # The followig example uses sychroized blocks public void trasfer ( it amout, Accout toaccout ) { boolea choice = this.serialnum() <= toaccout. serialnum() ; sychroized( choice? this : toaccout ) { sychroized( choice? toaccout : this ) { if( balace >= amout ) { toaccout.addfuds( amout ) ; balace -= amout; else { 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 23
Testig Cocurret Programs You ca ot effectively test cocurret programs to show that they are error free Because of race coditios, a test may pass millios of times by chace. Tests that fail are useful. They tell us we have a bug. Tests that pass oly tell us that it is possible for the code to compute the correct result. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 24
Testig: A True Story I wated to illustrate how race coditios ca cause data corruptio. So I wrote a program with two threads sharig a it variable x. Iitially x was set to 0 Oe thread icremeted x a thousad times The other thread decremeted x a thousad times. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 25
Testig: A True Story class Icremetor exteds Thread { public void ru() { for(it i=0 ; i < 1000; ++i) ++x ; class Decremetor exteds Thread { public void ru() { for(it i=0 ; i < 1000; ++i) --x ; 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 26
Testig: A True Story I ra the two threads cocurretly System.out.pritl( "The iitial value of x is: " + x ) ; Thread p = ew Icremetor() ; Thread = ew Decremetor() ; p.start() ;.start(); // Wait for threads to fiish usig jois (ot show) System.out.pritl( "After "+1000+" icremets ad "+1000+" decremets" ) ; System.out.pritl( "the fial value of x is: " + x ) ; What do you thik happeed? 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 27
Testig: A True Story Here s the output: The iitial value of x is: 0 After 1000 icremets ad 1000 decremets the fial value of x is: 0 Eve though I deliberately wrote a faulty program ad gave it 1 thousad chaces to fail the test, it passed ayway.. I tried 10,000. The 100,000. Same result 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 28
Testig: A True Story Ad why did it pass? The JVM happeed to give each thread time slices so log that the icremetig thread completed its 100,000 icremets before the mai thread had a chace to eve start the decremetig thread. Chagig to 1,000,000 icremets ad decremets revealed the bug. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 29
Testig: A True Story I had falle victim to optimism. I had optimistically assumed that such a obvious bug would cause a thorough test to fail. If tests desiged to reveal blatat bugs, which we kow are i the program, fail to reveal them, should we expect testig to reliably reveal subtle bugs we do ot kow about? 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 30
If you ca t test, the what? The good ews is that although testig is isufficiet to reveal bugs, there are desig ad aalysis techiues that allow you to prove your programs correct. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 31
Cocurrecy 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. Race coditios Deadlock Isufficiecy of testig 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 32
Summary of termiology cocurrecy: multiple agets ruig at the same time, iteractig thread: a idepedet path of cotrol Thread: a Java class represetig threads race coditio: a hazard caused by the upredictability of executio timig sychroized access: lockig of objects to obtai exclusive access wait ad otifyall: threads may wait util otified deadlock: a cycle of threads mutually waitig for each other safety property: a property that says somethig (bad) will ever happe liveess property: a property that says somethig (good) will evetually happe 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 33