1 / 21

System Programming

System Programming. Practical Session 4: Concurrency / Safety. Safety. Running several threads in parallel is not safe. Unpredictable results on shared resources. Design aproaches towards avoiding safety problems Thread confinement Immutability Locking / Synchronization.

mimi
Download Presentation

System Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. System Programming Practical Session 4: Concurrency / Safety

  2. Safety • Running several threads in parallel is not safe. • Unpredictable results on shared resources. • Design aproaches towards avoiding safety problems • Thread confinement • Immutability • Locking / Synchronization

  3. Shared Resources Shared resource - A resource that is visible to several threads. Objects that may be visisble to several threads - Are not safe. Objectst that cannot be shared (local function variables) - Are safe.

  4. classFoo{ • publicstaticdouble • NUMBER=33.3;} • classClassA{ • publiclongi; • publicVectorv; • publicClassA (){/*…*/} • publicvoiddoSomething( longx,Listlst){ • longlocalVar=0; • Objecto; • o=this.v.elementAt(2); //1 • localVar+=2;//2 • this.i+=2;//3 • x+=2;//4 • lst.elementAt(2);//5 • Foo.NUMBER=4;//6 • localVar=Foo.NUMBER;//7 • } } • classTaskAimplementsRunnable{ • privateClassAa; • privateListlst; • publiclongr; • //… some code …. • publicvoidrun(){ • this.r=2;//8 • this.a.doSomething( • 9,lst);//9 • 8. } • 9. } • 10. classMain{ • 11. publicstaticvoidmain( String[]args){ • 13.ClassAo1=newClassA(); • Threadt1= • newThread(newTaskA(o1)); • 15. t1.start(); • //…some code…. } }

  5. Thread Confinement A resource that is used exclusively by a one single thread is confined to the thread. If all the resources of a method are confined, then it is safe.

  6. publicclassThreadConfinedExample • { • //ThreadConfined • publicCarcreateCar(){ • Enginee=newFuelEngine(); • List<Door>doors=newLinkedList<Door>(); • doors.add(newFrontDoor()); • doors.add(newFrontDoor()); • doors.add(newBackDoor()); • doors.add(newBackDoor()); • Radior=newAMFMRadio(); • Carc=newCar(e,doors,r); • returnc; • } • }

  7. publicclassThreadNotConfinedExample • { • //NotThreadConfined • publicCarcreateCar(Enginee){ • 5.List<Door>doors=newLinkedList<Door>; • 6. doors.add(newFrontDoor()); • 7 doors.add(newFrontDoor()); • 8. doors.add(newBackDoor()); • 9.doors.add(newBackDoor()); • Radior=newAMFMRadio()); • Carc=newCar(e,doors,r); • returnc; • } • }

  8. Immutability An immutable object’s state cannot be changed after construction. • Examples: • String • Integer

  9. Immutability An object is immutable if • All primitive fields are final. • All other fields are references to immutable objects. • The object has been safely published: • Reference to this hasn't escaped during construction. • No thread has accessed the object before the construction completed.

  10. ‘this’ escape example Public class ClassA{ ………….. public void setB(ClassB objB) { … } ……… } Public class ClassB{ …………… public ClassB(ClassA objA){ //constructor …………… objA.setB(this); } …………… }

  11. Immutability An object with references to mutable objects can still be immutable! A class will be immutable if all of the following are true: • All of its fields are final • The class is declared final • The this reference is not allowed to escape during construction • Any fields that contain references to mutable objects, such as arrays, collections, or mutable classes: • Are private • Are never returned or otherwise exposed to callers • Are the only reference to the objects that they reference • Do not change the state of the referenced objects after construction

  12. publicfinalclassThreeStooges{ privatefinalSet<String>stooges= newHashSet<String>(); publicThreeStooges(){ stooges.add("Moe"); stooges.add("Larry"); stooges.add("Curly"); } publicbooleanisStooge(Stringname){ returnstooges.contains(name); } }

  13. Synchronization A mechanism allowing safely accessing shared resources. A thread accessing a synchronized method locks the object. The object cannot be accessed by other threads while it is locked.

  14. Synchronization 1. Class Even{ 2. privatelong n = 0; 3. publiclongnext(){ 4. n++; 5. n++; 6.returnn; 7. } 8. }

  15. Synchronization 1. Class Even{ 2. privatelong n = 0; 3. publicsynchronizedlongnext(){ 4. n++; 5. n++; 6.returnn; 7. } 8. }

  16. Synchronizing a block • publicintfoo(){ • //dosomething safe • synchronized(this){ • //do something • return9; • } • }

  17. Solution to safety problem Printer Example (from practical session 1) • classPrinter{ • Printer(){} • /** • *@paramilinenumber • *@paramsthestringtoconcatenate 40 times • */ • publicsynchronizedvoidprintALine(inti,Strings){ • System.out.print(i+")"); • for(intj=0;j<40;j++){ • System.out.print(s); • } • System.out.println(); • } • }

  18. publicclassGoodSynchronization{ • publicstaticvoidmain(String[]a){ • Printerp=newPrinter(); • Threadt1=newThread(newSimpleAsynchronousTask("a",p)); • Threadt2=newThread(newSimpleAsynchronousTask("b",p)); • t1.start();//printssomelinesofaaaa • t2.start();//printssomelinesofbbbb • } } • classSimpleAsynchronousTaskimplementsRunnable{ • Printerm_p; • Stringm_name; • publicSimpleAsynchronousTask(Stringname,Printerp){ • m_p=p; • m_name=name; • } • publicvoidrun(){ • for(inti=0;i<50;i++){ • m_p.printALine(i,m_name); • } • } }

  19. Wrong solution #1 • publicclassBadSynchronization2 • publicstaticvoidmain(String[]a){ • Printerp1=newPrinter(); • Printerp2=newPrinter(); • Threadt1=newThread(newSimpleAsynchronousTask("a",p1)); • Threadt2=newThread(newSimpleAsynchronousTask("b",p2)); • t1.start();//printssomelinesofaaaa • t2.start();//printssomelinesofbbbb • } • } • classSimpleAsynchronousTaskimplementsRunnable{ • .....thesamelikeGoodSynchronization.java • classPrinter{ • .....thesamelikeGoodSynchronization.java • }

  20. Wrong solution #2 (no printer object) • classBadSynchronization{ • publicstaticvoidmain(String[]a){ • Threadt1=newThread(newSimpleAsynchronousTask("a")); • Threadt2=newThread(newSimpleAsynchronousTask("b")); • t1.start();//printssomelinesofaaaa • t2.start();//printssomelinesofbbbb • } • } • classSimpleAsynchronousTaskimplementsRunnable{ • .....thesamelikeGoodSynchronization.java • publicsynchronizedvoidprintALine(inti,Strings){ • System.out.print(i+")"); • for(intj=0;j<40;j++)System.out.print(s); • System.out.println(); • } • }

  21. Summary • The most useful policies for using and sharing objects in a concurrent program are: • Thread-confined • Shared read-only • Shared thread-safe

More Related