ICLP c3 Java3
ICLP c3 Java3
programare
JAVA 3
FMI @ UNIBUC
1
Synchronization using class Lock
Readers-Writers Model
Sequential consistency
2
Synchronization using class Lock
Lock vs synchronized
3
Avoiding interference using Lock
4
interface Lock
5
interface Condition
6
Better Producer-Consumer Solution using Lock and Condition
(I)
public Cell() {
cellLock = new ReentrantLock();
putCond = cellLock.newCondition();
getCond = cellLock.newCondition();
}
7
Better Producer-Consumer Solution using Lock and Condition
(put)
8
Better Producer-Consumer Solution using Lock and Condition
(take)
9
Better Producer-Consumer Solution using Lock and Condition
(close)
Notes
• using different conditions for put and take
• Advantage: no need to wake all threads each time something
changes
10
Readers-Writers Model
Readers-Writers Interaction Model
11
Addressing non-interference using ReadWriteLock (reader
thread)
12
Addressing non-interference using ReadWriteLock (writer
threads)
13
Addressing non-interference using ReadWriteLock (main setup)
incThread.start();
decThread.start();
ExecutorService executorService =
Executors.newFixedThreadPool(2);
for (int i = 0; i < 100; i++)
executorService.execute(reader);
executorService.shutdown();
while (!executorService.isTerminated()) {
executorService.awaitTermination(1, TimeUnit.SECONDS);
}
incThread.join();
decThread.join();
System.out.println("c = " + c);
}
}
14
Executor, ExecutorService,
Executors
Executor and ExecutorService interfaces
15
class Executors
16
Asynchronous functions in Java: Callable, Future
Callable<T>
• Abstraction for actions (concurrent or not) returning a result
• think of it as a Runnable returning a value.
Callable<Integer> giveMeFive = () -> 5;
Future<T>
The promise of an Executor to deliver the result of a Callable
T get() Obtains the result promised by the Future. May block
waiting for the action to conclude
17
Future
18
Future example using isDone
19
ExecutorService: invokeAll and invokeAny
List<Future<T>>
20
invokeAll example
Optional<To> mapReduce(Collection<From> c,
Function<From, To> map,
BinaryOperator<To> reduce)
throws Exception {
return
executor.invokeAll(
c.stream()
.map( e -> ((Callable<To>) () -> map.apply(e)) )
.collect(Collectors.toList())
).stream()
.map(InvokeAllExample::get)
.reduce(reduce);
}
21
invokeAny example
22
Sequential consistency
Concurrent objects and serial specification
Concurrent objects
Shared entities upon which a thread can perform certain operations
Serial specification
The valid behavior of a concurrent object (in isolation)
• we only observe the operations performed on that object
23
Concurrent object example: shared memory location
Operations
read Reading the value from the memory location
write Writing a value into the memory location
Serial specification
Every read operation yields the value written by the latest write.
24
Concurent object example: mutex (lock)
Operations
lock acquires the lock
unlock releases the lock
Serial specification
• #lock - #unlock is either 0 or 1 for all prefixes
• sequence of lock unlock pairs
• last unlock can miss
• Consecutive lock - unlock operations belong to same thread
25
Formalizing sequential consistency [Attiya&Welch, 1994]
Legal execution
• Any concurrent object satisfies its serial specification
• When restricting the execution to that object alone.
Execution reordering
A permutation of the events in the original execution such that its
restriction to each thread is the same as in the original execution
27
Synchronization order
28