Download presentation
Presentation is loading. Please wait.
1
The Joy of Synchronization Lecture 19, Nov 5
2
Administrivia Reminder: Schedule P3M1 with us Only 3 groups scheduled so far… Time slots running out...
3
Wheel of Time (god I hope not...) Last time: Multiprocessing intro: thread/process machine models Intro to Graphics2D: the basic graphics pipeline Geometry and rendering intro Today: Threading reprised More on geometry and affine transforms
4
The joy of synchronization
5
Synchronization Can’t control when/how long each thread runs Can ensure that threads don’t work on same data at same time
6
Synchronization Definition: critical section Segment of code that should not be invoked by >1 thread at a time
7
Synchronization Definition: Mutual exclusion Only 1 thread can be executing block (critical section) at a time
8
Synchronization Definition: Lock (monitor) Data struct used to ensure mutual exclusion w/in critical section
9
Kinds of exclusion 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.
10
Live Action Multithreading Demo
11
public class BoxObject { private int[] DataArray=new int[4]; public void twiddle(int c) { for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; } BoxObject X=new BoxObject(); BoxObject Y=new BoxObject();
12
public class BoxObject { private int[] DataArray=new int[4]; public void twiddle(int c) { for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; } BoxObject X=new BoxObject(); BoxObject Y=new BoxObject(); Thread A:Thread B: X.twiddle(3)Y.twiddle(5)
13
X.DataArray={ 0, 3, 6, 9 } Y.DataArray={ 0, 5, 10, 15 }
14
public class BoxObject { private int[] DataArray=new int[4]; public void twiddle(int c) { for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; } BoxObject X=new BoxObject(); BoxObject Y=new BoxObject(); Thread A:Thread B: X.twiddle(3)X.twiddle(5)
15
X.DataArray={ 0, 3, 6, 9 } or, maybe... X.DataArray={ 0, 5, 10, 15 } or, maybe... X.DataArray={ 0, 5, 6, 9 } or even... X.DataArray={ 0, 3, 10, 9 }
16
public class BoxObject { private int[] DataArray=new int[4]; private Lock _l=new Lock(); }
17
public class BoxObject { private int[] DataArray=new int[4]; private Lock _l=new Lock(); public void twiddle(int c) { _l.retrieve(); for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; } _l.release(); }
18
public class BoxObject { private int[] DataArray=new int[4]; private Lock _l=new Lock(); public void twiddle(int c) { _l.retrieve(); for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; } _l.release(); } Thread A:Thread B: X.twiddle(3)Y.twiddle(5)
19
public class BoxObject { private int[] DataArray=new int[4]; private Lock _l=new Lock(); public void twiddle(int c) { _l.retrieve(); for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; } _l.release(); } Thread A:Thread B: X.twiddle(3)X.twiddle(5)
20
public class BoxObject { private int[] DataArray=new int[4]; private static int[] StaticData= new int[4]; private Lock _l=new Lock(); }
21
public class BoxObject { private int[] DataArray=new int[4]; private static int[] StaticData= new int[4]; private Lock _l=new Lock(); public void twiddle(int c) { _l.retrieve(); for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; StaticData[i]=-c*c*i*i; } _l.release(); }
22
Back to Java: Synchronization
23
The (dirty) truth of synchronized Java synchronized keyword only means: Get the lock before entering block Release lock when leaving block Trick is: which lock do you mean?
24
Context Switch
26
Back to Graphics
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.