advanced concurrent programming in java shared...
TRANSCRIPT
![Page 1: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/1.jpg)
Advanced concurrent programming in JavaShared objects
Mehmet Ali Arslan
21.10.13
1
![Page 2: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/2.jpg)
Visibility
To see(m) or not to see(m)...
There is more to synchronization than just atomicity or criticalsessions.Memory visibility... Updates by one thread to a shared objects statemust be visible to the others.Without proper synchronization, reordering can mess up the view.
Stale data: out-of-date value
2
![Page 3: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/3.jpg)
Visibility
To see(m) or not to see(m)...
There is more to synchronization than just atomicity or criticalsessions.Memory visibility... Updates by one thread to a shared objects statemust be visible to the others.Without proper synchronization, reordering can mess up the view.
Stale data: out-of-date value
2
![Page 4: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/4.jpg)
Visibility
synchronized and visibility
We can use intrinsic locks to ensure correct visibility.
...acts like a barrier.
3
![Page 5: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/5.jpg)
Visibility
synchronized and visibility
We can use intrinsic locks to ensure correct visibility.
...acts like a barrier.
3
![Page 6: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/6.jpg)
Visibility
synchronized and visibility
We can use intrinsic locks to ensure correct visibility.
...acts like a barrier.3
![Page 7: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/7.jpg)
Visibility
volatile
Weaker form of synch.To compiler and runtime: ”Do not reorder with other memory ops!””...a read of a volatile variable always returns the most recent writeby any thread.”No locking → lighter than synchronized
Does not guarantee atomicity!
Use only when:writes don’t depend on the current value or only a single threadever updates.the variable does not participate in invariants with other state vars.locking is not required for any other reason
4
![Page 8: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/8.jpg)
Visibility
volatile
Weaker form of synch.To compiler and runtime: ”Do not reorder with other memory ops!””...a read of a volatile variable always returns the most recent writeby any thread.”No locking → lighter than synchronized
Does not guarantee atomicity!
Use only when:writes don’t depend on the current value or only a single threadever updates.the variable does not participate in invariants with other state vars.locking is not required for any other reason
4
![Page 9: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/9.jpg)
Publication and escape
Definitions
Making an object available out of its current scope is called publishingit. Examples of publication:
public
any objects referred to as non-private fields of a published objectan object passed to an alien method i.e. a method whosebehavior is not fully specified by the respective object (includes itsoverrideable methods as well).
An object that is published when it shouldn’t have been is escaped.
5
![Page 10: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/10.jpg)
Publication and escape
Definitions
Making an object available out of its current scope is called publishingit. Examples of publication:
public
any objects referred to as non-private fields of a published objectan object passed to an alien method i.e. a method whosebehavior is not fully specified by the respective object (includes itsoverrideable methods as well).
An object that is published when it shouldn’t have been is escaped.
5
![Page 11: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/11.jpg)
Publication and escape
Escape under construction/Safe construction
An object is in a consistent state only after its constructor returns.Publication before that is hazardous.Some examples that would lead this reference to escape:
starting a thread in the constructorcalling an overrideable instance method in the constructor that isneither private nor final
6
![Page 12: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/12.jpg)
Publication and escape
Escape under construction/Safe construction
An object is in a consistent state only after its constructor returns.Publication before that is hazardous.Some examples that would lead this reference to escape:
starting a thread in the constructorcalling an overrideable instance method in the constructor that isneither private nor final
6
![Page 13: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/13.jpg)
Thread confinement
To share or not to share... - No publication
When an object is confined to a thread, safety is guaranteed. Even ifthe object itself is not thread-safe. Programmer is responsible toensure that the confined objects do not escape from the thread.
Ad-hoc - no language feature is used. Often used forimplementing a single-threaded subsystem.Stack - confine objects as local variablesThreadLocal - every thread gets its own value-holding object,not shared with others.
7
![Page 14: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/14.jpg)
Immutability
No mutable, no cry
State cannot be changed after construction = immutableAlways thread-safe. No worries about publishing.Two more conditions for an object to be immutable:
all fields are final
properly constructed (no escape under construction)
8
![Page 15: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/15.jpg)
Safe publication
Safe vs. improper publication
A publication is safe when the published object is correctly visibleat publication time - regards initialization of the object.Both the reference of the object and the object’s state must bepublished at the same time.Even if the object itself is thread-safe, if the reference to it ispublished without sufficient synch., this will cause visibilityproblems thus, improper publication.JavaMemory Model guarantees initialization safety for immutables.
9
![Page 16: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/16.jpg)
Safe publication
How to publish a properly constructed object
Properly constructed - no escape in constructorSome safe publication methods:
Init the reference from a static initializer - safety guaranteed byJVMStore a reference into a volatile field or AtomicReferenceStore a reference to it in a final field of another properlyconstructed objectStore a reference to it in a field that is guarded by a lock
10
![Page 17: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/17.jpg)
Safe publication
Sharing objects safely
Safe publication ensures only the visibility of the as-published state →synch. is necessary for every access to shared mutable objects.
“Rules of engagement”: when publishing an object, document how itcan be accessed-regarding mutability, synch. methods, etc.
Some common policies for sharing objects:Thread-confined: no thread interaction for the respective objectShared read-only: immutable and effectively immutable objectsShared thread-safe: object itself is responsibleGuarded: can be accessed only with a specific lock held
11
![Page 18: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/18.jpg)
Safe publication
Sharing objects safely
Safe publication ensures only the visibility of the as-published state →synch. is necessary for every access to shared mutable objects.
“Rules of engagement”: when publishing an object, document how itcan be accessed-regarding mutability, synch. methods, etc.
Some common policies for sharing objects:Thread-confined: no thread interaction for the respective objectShared read-only: immutable and effectively immutable objectsShared thread-safe: object itself is responsibleGuarded: can be accessed only with a specific lock held
11
![Page 19: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/19.jpg)
Safe publication
Exercise 1
12
![Page 20: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/20.jpg)
Safe publication
Exercise 2
13
![Page 21: Advanced concurrent programming in Java Shared objectsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch3-presentation.pdf · Advanced concurrent programming in Java Shared objects](https://reader036.vdocument.in/reader036/viewer/2022062317/5f0bdef07e708231d4329fa2/html5/thumbnails/21.jpg)
Safe publication
Thanks for the attention!
14