Presentation is loading. Please wait.

Presentation is loading. Please wait.

In Sync (No, not the lame boy band.). Today in history... Last time: Thinking about design: interface design Survey of code complexity: P1 vs. P3 Intro.

Similar presentations


Presentation on theme: "In Sync (No, not the lame boy band.). Today in history... Last time: Thinking about design: interface design Survey of code complexity: P1 vs. P3 Intro."— Presentation transcript:

1 In Sync (No, not the lame boy band.)

2 Today in history... Last time: Thinking about design: interface design Survey of code complexity: P1 vs. P3 Intro to the thread model: procs vs. threads This time: Team tips Notes on presentations Synchronization & locking

3 News in the Real World... Privacy Int’l & EPIC: 2006 Human Rights Study released Summary report: Ranking of surveillance societies US ranked 33rd... Between Israel & Thailand...

4 Civics! Nov 7 (next Tues)==election day Vote! Pseudo-code: NMGovt.registerToVote(this); this.educate(webPtr,newsPaperPtr); Location l=findPollingPlace(this); // on Nov 7: PollingPlace p=this.relocate(l); p.castVotes(new HashMap ());

5 Possibly useful resources... League of Women Voters info pages: http://www.lwvnm.org/ http://www.lwvnm.org ABQ Journal: http://www.abqjournal.com / http://www.abqjournal.com ABQ Tribune: http://www.abqtrib.com / http://www.abqtrib.com Google news: http://news.google.com/ http://news.google.com Party pages (link to candidates & platforms): http://www.nmdemocrats.org/ http://www.nmdemocrats.org http://www.gopnm.org/ http://www.gopnm.org http://www.greenpartynm.org/ http://www.greenpartynm.org And, of course, your local library...

6 Election day deals I will be here to lecture Iff you need this time block to vote, you may go vote rather than coming to class I won’t give any quizzes or assignments... Nov 7 is also rollout date for P3M1 You may have until 2:00 PM on Wed, Nov 8 for P3M1

7 Administrivia P3M1 coming up... Whole group should meet w/ me and Blake in person You MUST arrange a time with me Allow 1 hour >= 1/2 hour on “formal presentation”; the rest for Q&A

8 Team tip: code ownership Who “owns” a module? Who is responsible for it? Model 1: Strong individual ownership One person is exclusive owner of each module  Knows it inside out  Can easily fix bugs, etc.  Clear-cut lines of responsibility  Each developer has limited responsibility  All changes have to pass through one person  Can be a serious bottleneck

9 Team tip: code ownership Loose ownership model Whole code base is “owned” by group Indivs may work primarily on one aspect, but anybody has rights to change anything  Fewer bottlenecks  More responsive to individual needs  Individuals have to be generalists, not specialists  Less familiarity ⇒ very easy to introduce bugs Esp. integration bugs Regression testing critical

10 About presentations... Goal of presentation: convey information clearly and concisely #1 most important thing: lay out your presentation ahead of time #2 most important: practice!

11 Presentation hints Formal presentation to an audience Avoid slang, informalism, etc. (Jokes ok, but don’t lose focus) Trying to convince audience of something E.g., you have a brilliant design that will address all issues flawlessly... Subtext: you’re smart and they should invest in you/your project

12 Presentation hints Think of it as teaching the audience about your design Get the important core ideas/designs across Don’t dwell on tedious detail Don’t be fluffy Be sure to weigh pros/cons (define/defend/attack)

13 Presentation hints Practice! Time will be tight -- time yourself Get friends/colleagues to help you practice Practice! Think about order of material presentation Practice!

14 Presentation hints Avoid using every clever powerpoint trick And be careful with cute, but pointless images

15 Presentation hints Oh, and avoid using bizarre fonts and really tiny font sizes just so that you can cram as much junk on the screen as possible. Remember: it’s more important that the audience actually understand your material than that you convey more ‘volume’ of material in the same time. It’s essentially pointless to ream through bunches of text or incredible amounts of math if nobody in the audience gets it. At best, they will be bored and zone out for most of your talk. At worst, they will be actively put off or annoyed by your presentation. And, presumably, you want them all to like you and be impressed with your material and ideas, so it’s counterproductive to antagonize your audience. Remember: at some point, your project, future funding, and/or job may depend on a presentation like this, so it behooves you to keep your audience happy. I have actually seen people give abysmally bad presentations and be completely rejected from the job opening because of their poor presentations. Now that that has been said, I still need to fill out this page with a large blob of text so that it’s as intimidating as possible. Honestly, I don’t expect anybody to actually read this far even in the online copy, let alone in class. If you do actually get this far while I’’m flashing this page up in class, do please shout out. I’ll be most impressed and you’ll get brownie points for speed reading. Even if you happen to read this far in the online copy, please send me a note, just to satisfy my curiosity about who’s determined enough to get that far. Hm. Still half a page to fill. This is a pretty drastically condensed slide. Let’s see. Need more text. Maybe a little web mining... Ok, here we go: APRIL is the cruellest month, breeding / Lilacs out of the dead land, mixing / Memory and desire, stirring / Dull roots with spring rain. / Winter kept us warm, covering / Earth in forgetful snow, feeding / A little life with dried tubers. / Summer surprised us, coming over the Starnbergersee / With a shower of rain; we stopped in the colonnade, / And went on in sunlight, into the Hofgarten, / And drank coffee, and talked for an hour. / Bin gar keine Russin, stamm' aus Litauen, echt deutsch. / And when we were children, staying at the archduke's, / My cousin's, he took me out on a sled, / And I was frightened. He said, Marie, / Marie, hold on tight. And down we went. / In the mountains, there you feel free. / I read, much of the night, and go south in the winter. / / What are the roots that clutch, what branches grow / Out of this stony rubbish? Son of man, / You cannot say, or guess, for you know only / A heap of broken images, where the sun beats, / And the dead tree gives no shelter, the cricket no relief, / And the dry stone no sound of water. Only / There is shadow under this red rock, / (Come in under the shadow of this red rock), / And I will show you something different from either / Your shadow at morning striding behind you / Or your shadow at evening rising to meet you; / I will show you fear in a handful of dust. / Frisch weht der Wind / Der Heimat zu. / Mein Irisch Kind, / Wo weilest du? / 'You gave me hyacinths first a year ago; / 'They called me the hyacinth girl.' / —Yet when we came back, late, from the Hyacinth garden, / Your arms full, and your hair wet, I could not / Speak, and my eyes failed, I was neither / Living nor dead, and I knew nothing, / Looking into the heart of light, the silence. / Od' und leer das Meer.

16 Presentation hints Oh yeah. Don’t switch slides too quickly.

17 Presentation hints Be sure to look at audience Don’t just read from your slides Don’t stare at screen whole time Be careful w/ laser pointers Practice!

18 Synchronization Can’t control when/how long each thread runs Can ensure that threads don’t work on same data at same time

19 Synchronization Definition: critical section Segment of code that should not be invoked by >1 thread at a time

20 Synchronization Definition: Mutual exclusion Only 1 thread can be executing block (critical section) at a time

21 Synchronization Definition: Lock (monitor) Data struct used to ensure mutual exclusion w/in critical section

22 Definition: enforced mutual exclusion You don’t have any choice. The system (OS, DB, etc.) forces mutex on the threads Definition: advisory mutual exclusion Everybody has to play nice together. It is up to the code to ensure that mutex is met.

23 The truth of synchronized Java synchronized keyword just means: get lock before proceeding; release lock when done

24 The truth of synchronized public class Panopticon { private double[] _data; public synchronized void set(int idx, double v) { _data[idx]=v; } public synchronized double get(int idx) { return _data[idx]; }

25 The truth of synchronized public class Panopticon { private double[] _data; hidden Object _lockID_=null; public void set(int idx, double v) { hidden Thread ct=Thread.currentThread(); while (!_testAndSet_((_lockID_==null || _lockID_==ct), _lockID_=ct); _data[idx]=v; _atomicSet_(_lockID_=null); } public double get(int idx) { hidden Thread ct=Thread.currentThread(); while (!_testAndSet_((_lockID_==null || _lockID_==ct), _lockID_=ct); int result=_data[idx]; _atomicSet_(_lockID_=null); return result; }

26 The truth of synchronized Java synchronized keyword just means: get lock before proceeding; release lock when done synchronized method: only one thread can execute this method at a time (*) synchronized block: only one thread can execute this block at a time (*)

27 Where is the lock? Caveat: synchronized methods/blocks are only mutex with respect to a single lock Have to make sure that all threads are using the same lock

28 Where is the lock? Where does the lock variable live? non-static synchronized methods: lives on the object instance static synchronized methods: lives on the class variable synchronized block: lives on the object named in the argument

29 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; }

30 Who owns the lock? The memory picture:

31 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } }

32 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } } Answer: “ this ” owns it (object on which getX0() is being called)

33 Who owns the lock? The memory picture:

34 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } public List getX1() { synchronized(x){return x; }} }

35 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } public List getX1() { synchronized(x){return x; }} } Answer: x itself owns the lock

36 Who owns the lock? The memory picture:

37 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } public List getX1() { synchronized(x){return x; }} public List getX2() { synchronized(this) { return x; } } }

38 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } public List getX1() { synchronized(x){return x; }} public List getX2() { synchronized(this) { return x; } } } Again, “ this ” owns it (duh)

39 Who owns the lock? The memory picture:

40 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } public List getX1() { synchronized(x){return x; }} public List getX2() { synchronized(this) { return x; } } public synchronized List getX3() { synchronized(x) { return x; } } }

41 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } public List getX1() { synchronized(x){return x; }} public List getX2() { synchronized(this) { return x; } } public synchronized List getX3() { synchronized(x) { return x; } } } Trick question! Both locks ( this and x ) have to be acquired!

42 Who owns the lock? The memory picture:

43 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getA0() { return a; } }

44 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getA0() { return a; } } Same old, same old. “ this ” has the lock.

45 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getA0() { return a; } public int getA1() { synchronized(a){ return a; }} }

46 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getA0() { return a; } public int getA1() { synchronized(a){ return a; }} } Another trick question! a is atomic -- it’s not an Object, and doesn’t have hidden state.

47 Who owns the lock? The memory picture: ???

48 Who owns the lock? Error you get from trying to synchronize on an atomic field: WhoOwnsTheLock.java:12: incompatible types found : int required: java.lang.Object public int getA1() { synchronized(a) { return a; } } ^ 1 error

49 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } }

50 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } } Again, “ this ” owns the lock.

51 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } } Again, “ this ” owns the lock. Danger Will Robinson! Can have multiple access to y !!!

52 Who owns the lock? Consider: WhoOwnsTheLock foo=new WhoOwnsTheLock(); WhoOwnsTheLock bar=new WhoOwnsTheLock();

53 Who owns the lock?

54 Consider: WhoOwnsTheLock foo=new WhoOwnsTheLock(); WhoOwnsTheLock bar=new WhoOwnsTheLock(); // in thread 0 foo.getY0(); // in thread 1 bar.getY0();

55 Who owns the lock? thread 0 synch w/ this lock

56 Who owns the lock? thread 0 synch w/ this lock & thread 1 synch w/ this one

57 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } public static synchronized List getY1() { return y; }

58 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } public static synchronized List getY1() { return y; } Remember: “ static ”==“associated with class” (i.e., class object). So this is synchronized on the class object. No more conflict. (yay!)

59 Who owns the lock?

60 public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } public static synchronized List getY1() { return y; } public static List getY2() { synchronized(y) { return y; } }

61 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } public static synchronized List getY1() { return y; } public static List getY2() { synchronized(y) { return y; } } Effectively, just as good as previous version...

62 Who owns the lock?

63 public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getXY0() { return x.size()+y.size(); }

64 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getXY0() { return x.size()+y.size(); } Synchronized on “ this ”, but can have concurrent access via y ( x is safe)

65 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getXY0() { return x.size()+y.size(); } public static synchronized int getXY1() { return x.size()+y.size(); }

66 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getXY0() { return x.size()+y.size(); } public static synchronized int getXY1() { return x.size()+y.size(); } Same problem, but in reverse -- y is safe, but can have multi-access to x.

67 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getXY0() { return x.size()+y.size(); } public static synchronized int getXY1() { return x.size()+y.size(); } public int getXY2() { synchronized(this) { // or on x itself synchronized(y) { return x.size()+y.size(); } } } }

68 Who owns the lock?

69

70 public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public int getAB0() { synchronized(this) { synhronized(b) { // oh oh... b is atomic return a+b; } } } }

71 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public int getAB0() { synchronized(this) { synhronized(y) { return a+b; } } } } Answer 1: synchronize on something “near” b (i.e., something else static) that isn’t, itself, atomic. Sometimes introduce spurious “ Object ” just for this

72 Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public static synchronized int getAB1() { synchronized(this) { return a+b; } } } Answer 2: synchronize on class object (via “ static synchronized ” method declaration)


Download ppt "In Sync (No, not the lame boy band.). Today in history... Last time: Thinking about design: interface design Survey of code complexity: P1 vs. P3 Intro."

Similar presentations


Ads by Google