Reading note: 7 concurrency models in 7 weeks - Threads and Locks
- Problem: race condition when accessing shared memory, JVM/compiler/hardware
optimize the code, causing change in logic for multiple-threads-program
- Solution: instrinsic lock -
- 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
- 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
- The lock is separated into
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.