Parallel Programming Practice Sharing Objects Susanne Cech - - PowerPoint PPT Presentation

parallel programming practice
SMART_READER_LITE
LIVE PREVIEW

Parallel Programming Practice Sharing Objects Susanne Cech - - PowerPoint PPT Presentation

Parallel Programming Practice Sharing Objects Susanne Cech Previtali Thomas Gross Last update: 2009-10-22, 13:02 Publication An object is published when It has been made available outside of its current scope How? Store a reference


slide-1
SLIDE 1

Sharing Objects

Parallel Programming Practice

Susanne Cech Previtali Thomas Gross

Last update: 2009-10-22, 13:02

slide-2
SLIDE 2

2102: Parallel Programming Practice, HS 2009 2

Publication

An object is published when

  • It has been made available outside of its current scope

How?

  • Store a reference where other code can access it
  • Return a reference from a non-private method
  • Pass a reference to a method in another class
slide-3
SLIDE 3

2102: Parallel Programming Practice, HS 2009

Properties

Publishing one object also publishes all its reachable objects

  • Follow chain of references
  • “Alien” method calls of a class C with object as argument
  • Methods in other classes
  • Overridable methods of C

Any method which is not private, static, or final can be overridden.

3

@ThreadSafe public class CachedFactorizer implements Servlet { @GuardedBy(“this”) private BigInteger lastNumber; @GuardedBy(“this”) private BigInteger[] lastFactors; public void service(ServletRequest req, ServletResponse resp) { BigInteger i = extractFromRequest(req); BigInteger[] factors = null; synchronized (this) { if (i.equals(lastNumber)) factors = lastFactors.clone(); } if (factors == null) { factors = factor(i); synchronized (this) { lastNumber = i; lastFactors = factors.clone(); } } encodeIntoResponse(resp, factors); } }

http://java.sun.com/javaee/5/docs/api/javax/servlet/Servlet.html

slide-4
SLIDE 4

2102: Parallel Programming Practice, HS 2009

Object graph of CachedFactorizer

4

BigInteger BigInteger BigInteger

lastNumber lastFactors

CachedFactorizer BigInteger

returned reference

BigInteger

this

BigInteger

is immutable

slide-5
SLIDE 5

2102: Parallel Programming Practice, HS 2009

Problems with escaped objects

An object is escaped when

  • It is published and should not have been published

Consequences

  • Any caller can modify object

5

slide-6
SLIDE 6

2102: Parallel Programming Practice, HS 2009

Proper construction

Object is not properly constructed if this escapes during construction

  • Consistent state only after constructor returns

Do not

  • Start a thread in the constructor
  • Call a overridable method in the constructor

6

slide-7
SLIDE 7

2102: Parallel Programming Practice, HS 2009

How to prevent escape

Thread confinement Immutability Safe publication

7

slide-8
SLIDE 8

2102: Parallel Programming Practice, HS 2009

Thread confinement

8

slide-9
SLIDE 9

2102: Parallel Programming Practice, HS 2009

Thread confinement

Avoid escaping of objects by not sharing Thread confinement

  • A single thread accesses data ⇒ thread safe

Kinds

  • Ad-hoc thread confinement
  • Stack confinement
  • ThreadLocal

9

slide-10
SLIDE 10

2102: Parallel Programming Practice, HS 2009

1 Ad-hoc thread confinement

Implementation is responsible

  • Fragile

Special case: volatile variables

  • Ensure that only one thread writes the volatile variable
  • Remember visibility guarantees of volatile writes

10

slide-11
SLIDE 11

2102: Parallel Programming Practice, HS 2009

2 Stack confinement

Object is reachable only through local variables

  • Local variables exist only on stack
  • Stack accessible only to current thread

Enforcement

  • Obvious for primitive types (no reference)
  • References: Programmer must take care and not publish reference

11

slide-12
SLIDE 12

2102: Parallel Programming Practice, HS 2009 12

public int loadTheArk(Collection<Animal> candidates) { SortedSet<Animal> animals = new TreeSet<Animal>(new SpeciesGenderComparator()); animals.addAll(candidates); int numPairs = 0; Animal candidate = null; for (Animal a : animals) { if (candidate == null || !candidate.isPotentialMate(a)) candidate = a; else { ark.load(new AnimalPair(candidate, a)); numPairs++; candidate = null; } } return numPairs; }

slide-13
SLIDE 13

2102: Parallel Programming Practice, HS 2009 13

candidate a animals ark loadedAnimals

Ark Animals

sp: ELEPHANT g: MALE sp: FROG g: MALE sp: FROG g: FEMALE candidates

Animal

species gender

HashSet

final not final

Animals.loadTheArk(Collection<Animal> candidates) this numPairs

  • ne

two

AnimalPair 1 :1 TreeSet

slide-14
SLIDE 14

2102: Parallel Programming Practice, HS 2009

3 ThreadLocal

Associate a per-thread value with an object

  • Separate copy of a value for each thread
  • Conceptual: Map<Thread, T>

Examples

  • Mutable singletons, global variables

14

slide-15
SLIDE 15

2102: Parallel Programming Practice, HS 2009

ThreadLocal API

java.lang.ThreadLocal<T>

15

T get()

Value of the current thread’s copy.

if value == null: return initialValue() T initialValue()

Typically overridden (default: return null;)

void remove()

Remove value of copy of current thread.

void set(T value) Set copy of current thread to value.

slide-16
SLIDE 16

2102: Parallel Programming Practice, HS 2009

Corrected ThreadLocal example

16

public class UniqueThreadIdGenerator { private static final AtomicInteger uniqueId = new AtomicInteger(0); private static final ThreadLocal<Integer> uniqueNum = new ThreadLocal<Integer>() { protected Integer initialValue() { return uniqueId.getAndIncrement(); } }; public static int getCurrentThreadId() { return uniqueNum.get(); } }

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6475885 See also:

slide-17
SLIDE 17

2102: Parallel Programming Practice, HS 2009

Immutability

17

slide-18
SLIDE 18

2102: Parallel Programming Practice, HS 2009

Immutability

An object is immutable if

  • Its state cannot be modified after construction and
  • All its fields are final and
  • It is properly constructed
  • (this reference does not escape during construction)

Immutable objects are always thread-safe

  • No synchronization needed

18

slide-19
SLIDE 19

2102: Parallel Programming Practice, HS 2009

Attention 1

19

class A { final B b; } class B { C c; } class C { int x; } b c x: 2 3 x: 4

final not final Immutability ≠ declare all fields final

  • Final fields can hold references to mutable objects
  • An object with final fields can still be mutable
slide-20
SLIDE 20

2102: Parallel Programming Practice, HS 2009

Reference is immutable ≠ object is immutable

Attention 2

20

class B { C c; } class C { final int x; } c x: 2 x: 4

final not final

slide-21
SLIDE 21

2102: Parallel Programming Practice, HS 2009

Immutable example

21

@Immutable public final class ThreeFriends { private final Set<String> friends = new HashSet<String>(); public ThreeFriends() { friends.add("Moe"); friends.add("Larry"); friends.add("Curly"); } public boolean isFriend(String name) { return friends.contains(name); } }

Update state with replacing old object with a new one Set is mutable but ThreeFriends is designed not to be mutable

slide-22
SLIDE 22

2102: Parallel Programming Practice, HS 2009

Definition of immutability revisited

An object is immutable if

  • all public fields are final,
  • all public final reference fields refer to other immutable objects, and
  • constructors and methods do not publish references to any internal

state which is potentially mutable by the implementation.

22

slide-23
SLIDE 23

2102: Parallel Programming Practice, HS 2009

Weak atomicity for immutable objects

23

@ThreadSafe public class VolatileCachedFactorizer implements Servlet { private volatile OneValueCache cache = new OneValueCache(null, null); public void service(ServletRequest req, ServletResponse resp) { BigInteger i = extractFromRequest(req); BigInteger[] factors = cache.getFactors(i); if (factors == null) { factors = factor(i); cache = new OneValueCache(i, factors); } encodeIntoResponse(resp, factors); } }

slide-24
SLIDE 24

2102: Parallel Programming Practice, HS 2009

Immutable holder class for atomic data

24

@Immutable public class OneValueCache { private final BigInteger lastNumber; private final BigInteger[] lastFactors; public OneValueCache(BigInteger i, BigInteger[] factors) { lastNumber = i; lastFactors = Arrays.copyOf(factors, factors.length); } public BigInteger[] getFactors(BigInteger i) { if (lastNumber == null || !lastNumber.equals(i)) return null; else return Arrays.copyOf(lastFactors, lastFactors.length); } }

slide-25
SLIDE 25

2102: Parallel Programming Practice, HS 2009

Publishing immutable objects

Immutable objects can be used without synchronization But

  • When final fields refer to mutable objects, synchronization must be

used to access those objects

25

slide-26
SLIDE 26

2102: Parallel Programming Practice, HS 2009

JMM: Initialization safety

Properly constructed immutable objects can be shared across threads without synchronization All threads will see correct values set in the constructor of

  • Final fields and any variables reachable through a final field
  • If the object was properly constructed object

For objects with final fields, no reordering of

  • Writes in the constructor to final fields
  • Writes to variables reachable through these final fields
  • With initial load of a reference of a reference to that object

⇒ Values become “frozen” when constructor completes

26

slide-27
SLIDE 27

2102: Parallel Programming Practice, HS 2009

Initialization safety for immutable objects

27

@ThreadSafe public class SafeStates { private final Map<String, String> states; public SafeStates() { states = new HashMap<String, String>(); states.put("alaska", "AK"); states.put("alabama", "AL"); /*...*/ states.put("wyoming", "WY"); } public String getAbbreviation(String s) { return states.get(s); } }

String is immutable values that are reachable through final fields at the time the constructor finishes

slide-28
SLIDE 28

2102: Parallel Programming Practice, HS 2009

Safe publication

28

slide-29
SLIDE 29

2102: Parallel Programming Practice, HS 2009

Unsafe publication

Other threads might see

  • Stale value for holder (null or older value)
  • Up-to-date value for holder, but stale values for the state of holder

29

@NotThreadSafe public class UnsafeLazyInitialization { private static Resource resource; public static Resource getInstance() { if (resource == null) resource = new Resource(); // unsafe publication return resource; } }

No happens- before ordering

slide-30
SLIDE 30

2102: Parallel Programming Practice, HS 2009

Safe publication

Objects that are not immutable must be safely published

  • Synchronization of both the publishing and consuming thread

Establish a happens-before ordering between publishing and consuming thread

  • To ensure visibility

Synchronization is required if the object can be modified after publication

30

slide-31
SLIDE 31

2102: Parallel Programming Practice, HS 2009

Safe publication patterns

Reference and state of the object must be made visible at the same time Consider a properly constructed object

  • Initialize the reference with a static initializer
  • Store the reference into a volatile field or AtomicReference
  • Store the reference into a final field of a properly constructed object
  • Store the reference into a field that is properly guarded by a lock

31

slide-32
SLIDE 32

2102: Parallel Programming Practice, HS 2009

Eager safe initialization

Static initializers

  • Run after class loading but before class is used by any threads
  • Writes are visible to all threads automatically

Consider also factory implementation

32

@ThreadSafe public class SafeEagerInitialization { private static Resource resource = new Resource(); public static Resource getInstance() { return resource; } }

slide-33
SLIDE 33

2102: Parallel Programming Practice, HS 2009

Safe lazy initialization

33

@ThreadSafe public class SafeLazyInitialization { private static Resource resource; public synchronized static Resource getInstance() { if (resource == null) resource = new Resource(); return resource; } }

slide-34
SLIDE 34

2102: Parallel Programming Practice, HS 2009

Double-checked locking

34

public class DoubleCheckedLocking { private static Resource resource; public static Resource getInstance() { if (resource == null) { synchronized (DoubleCheckedLocking.class) { if (resource == null) resource = new Resource(); } } return resource; } } @NotThreadSafe

slide-35
SLIDE 35

2102: Parallel Programming Practice, HS 2009

Corrected double-checked locking

35

public class DoubleCheckedLocking { private volatile static Resource resource; public static Resource getInstance() { if (resource == null) { synchronized (DoubleCheckedLocking.class) { if (resource == null) resource = new Resource(); } } return resource; } } @ThreadSafe

Better: SafeLazyInitialization with factory (see Slide 33)

slide-36
SLIDE 36

2102: Parallel Programming Practice, HS 2009

Publishing and sharing

Immutable objects

  • Can be published through any mechanism
  • Shared without synchronization

Effectively immutable objects

  • == mutable objects that are not modified (e.g. Date)
  • Must be safely published
  • Shared without synchronization

Mutable objects

  • Must be safely published and
  • Must be either thread-safe or guarded by a lock

36

slide-37
SLIDE 37

2102: Parallel Programming Practice, HS 2009

Document accessibility of objects

Thread-confined

  • Owed by and confined to thread
  • Can be modified only by owning thread

Shared read-only

  • Cannot be modified
  • Access without synchronization

Shared thread-safe

  • Internal synchronization
  • Threads can use without synchronization using public interface

Guarded

  • Accessed only with specific lock

37

slide-38
SLIDE 38

2102: Parallel Programming Practice, HS 2009

Package net.jcip.annotations

38

Annotation Target Description

@ThreadSafe

Class No synchronization needed by

  • client. No interleaving of accesses

puts object in invalid state.

@NotThreadSafe

Class

@Immutable

Class State cannot be seen to change by

  • callers. Implies @ThreadSafe.

@GuardedBy(“lock”)

Field, method

lock must be used to access field/

method.

http://www.javaconcurrencyinpractice.com/annotations/doc/index.html

slide-39
SLIDE 39

2102: Parallel Programming Practice, HS 2009

Study Goals

39