11. Throughput with
synchronized
Throughput: 9
regions
wait...
wait...
9
12. Throughput with
synchronized
Throughput: 11
regions
wait...
wait...
wait...
9
13. Throughput with
synchronized
Throughput: 11 regions
wait... wait...
wait... wait... wait...
wait... wait...
wait... wait...
wait... wait...
10
14. Throughput with
synchronized
regions
Throughput: 11
wait...
wait... wait...
wait...
wait...
wait...
wait...
11
15. The fundamentals
๏Threads
๏Mutual exclusion
synchronization and locks
๏Java Memory Model
read/write barriers & publication
12
16. The Java Memory Model 1 33
What you need to know JSR
๏ Visibility / Publication of objects/fields
๏ (Un)Acceptable out-of-order behavior
i.e. guarantees about ordering
๏ Implementations based on the
hardware MM of the target platform
13
19. Threads
๏ Abstract the line of excution from the CPU(s)
๏ Provided by most (all mainstream) operating systems
๏ CPU execution can continue on another thread if
one thread blocks
๏ Increases CPU utilization
16
22. class Thing {
volatile String name;
String getName() {
return this.name; read barrier
}
void setName( String newName ) {
this.name = newName; write barrier
}
}
๏ Guarantees visibility:
always read the latest value
๏Guarantees safe publication:
no re-ordering of
pre-write ops with
post-write ops 19
23. Monitors
synchronized (someMonitor)
{ read barrier
// single threaded region
} write barrier
๏Writes may not be
reordered across the end
๏Reads may not be
reordered across the start
20
25. ConcurrentMap<String,Thing> map =
new ConcurrentHashMap();
๏Adds atomic operations to the Map
interface:
- putIfAbsent(key, value)
- remove(key, value)
- replace(key,[oldVal,]newVal)
- but not yet
putIfAbsent(key, #{makeValue()})
๏CHM is lock striped, i.e. synchronized
on regions 22
26. List<Thing> list = new
CopyOnWriteArrayList();
๏Synchronize writers,
unrestricted readers
๏Good for: #reads >> #writes
๏Readers get snapshot state:
gives stable iteration
๏Volatile variable for immutable
state to ensure publication 23
27. public class CopyOnWriteArrayList<T>
private volatile Object[] array = new Object[0];
public synchronized boolean add(T value) {
Object[] newArray = Arrays.copyOf(array, array.length+1);
newArray[array.length] = value;
array = newArray; // write barrier
return true;
} // write barrier
public Iterator<T> iterator() {
return new ArrayIterator<T>(this.array); // read barrier
}
private static class ArrayIterator<T> implements Iterator<T> {
// I’ve written too many of these to be amused any more ...
}
24
28. ExecutorService executor = new
ThreadPoolExecutor();
executor.submit(new Runnable() {
๏
void run() {
doWork(); Focus on tasks - not threads
}
}); ๏Mitigate thread start/stop
overhead
๏Ensure a balanced number
of threads for the target
platform
25
30. LockSupport.park(); // currentThread
LockSupport.unpark(Thread t);
๏The thread will “sleep” until unparked
๏Although unpark-before-park marks
the thread as unparked, returning
from park immediately
๏... and there’s still the chance of
spurious wakeups ...
๏Too low level for most people 27
31. Lock lock = new ReentrantLock();
ReadWriteLock rwLock = new ReentrantReadWriteLock();
Lock read = rwLock.readLock();
// Two related locks
Lock write = rwLock.writeLock();
lock.lock();
try { doWork() } finally { lock.unlock(); }
if ( lock.tryLock() )
try { doWork() } finally { lock.unlock(); }
else
backOffAndTryAgainLater();
28
36. ForkJoin
๏More “advanced” Executor
๏Assumes/requires more of
the tasks it runs
๏Tasks first split into multiple
sub-tasks, push on stack
๏Idle workers “steal” work
from the bottom of the
stack of other workers
32
43. class SimpleActor extends Actor {
var state
def receive = {
case Get => self reply state
case Set(newState) => state = newState
}
}
ge”
ans
ssa
“se s me
me
nd
i
Th
val response = simple !! Set( "new state" )
39
57. (def street (ref))
(def city (ref))
(defn move [new-street new-city]
(dosync ; synchronize the changes
(ref-set street new-street)
(ref-set city new-city)))
; will retry if txn fails due to concurrent change
(defn print-address []
(dosync ; get a snapshot state of the refs
(printf "I live on %s in %s%n" @street @city)))
50