Reading note: 7 concurrency models in 7 weeks - Threads and Locks
Jul 31 2018
Day 1
- Problem: race condition when accessing shared memory, JVM/compiler/hardware
optimize the code, causing change in logic for multiple-threads-program
- Solution: instrinsic lock -
synchronized
, volatile
keyword
- Problem with instrinsic lock: dead lock
- Solution: Acquire locks in a fixed, global order, do not call alien methods,
hold lock for short amount of time.
- Read more: Java memory model
Day 2
- Problem: the need of thread interruption, timeout when acquiring lock, control acquiring
and releasing lock
- Solution: Reentrant Lock, Atomic variables
- Read more:
- Fairness in Reentrant Lock
- ReentrantReadWriteLock
- The lock is separated into
read_lock
and write_lock
. This is for
optimizing for workload with a lot of read and sporadic write.
- Read and Write lock can also have fairness and timeout
- Read -> Write is upgraded, and Write -> Read is downgraded
- Spurious Wakup:
the event when a call to
wait
returns even if the condition variable does not update, which forces developers
to check the condition variable in a while loop. A good design software does
not need to care about this because it uses better locking mechanisim instead
of condition variable.
- AtomicReferenceFieldUpdater