文章地址: http://tutorials.jenkov.com/java-concurrency/index.html
Table of Contents
第一章 并发模型
1.1 Parallel Workers
1.2 Assembly Line
Reference: http://tutorials.jenkov.com/java-concurrency/concurrency-models.html
第二章 并发的本质
2.1.1 Race Condition (code block atomic)
要求被执行的代码块要么全部执行,要么都不执行。
Reference: http://tutorials.jenkov.com/java-concurrency/race-conditions-and-critical-sections.html
2.1.2 Variable Visibility (write to main memory, read from main memory)
对数据的写,不保存在cache里,而是直接刷新到内存;
对数据的读,不是从cache里读取,而是从内存里读取;
http://tutorials.jenkov.com/java-concurrency/java-memory-model.html
http://tutorials.jenkov.com/java-concurrency/volatile.html
2.2.1 blocking
race condition : synchronized
variable visibility: synchronized
synchronized method synchronized static method
synchronized instance synchronized Class instance
又叫悲观锁,适用于高并发场景。
http://tutorials.jenkov.com/java-concurrency/synchronized.html
2.2.2 unblocking
race condition: Unsafe.compareAndSwap
variable visibility: volatile (64位原子,写内存,读内存; write before, read after 指令禁止重排)
又叫乐观锁,适用于中低并发场景。
http://tutorials.jenkov.com/java-concurrency/compare-and-swap.html
http://tutorials.jenkov.com/java-concurrency/non-blocking-algorithms.html
第三章 CompareAndSwap常用类
3.1 Lock
ReentrantLock 可重入锁 (已经获得该类的锁,在critical area里需要访问同一个类的其他critical area)
ReentrantReadWriteLock 可重入读写锁
http://tutorials.jenkov.com/java-concurrency/locks.html
http://tutorials.jenkov.com/java-concurrency/read-write-locks.html
3.2 BlockingQueue
ArrayBlockingQueue
LinkedBlockingQueue
3.3 AtomicXxx
AtomicBoolean
AtomicInteger
AtomicLong
AtomicDouble
AtomicReference
第四章 Thread Signal
4.1 Object
wait 在一个monitor对象上block住,释放锁
notify 在monitor上block住的线程中随机选择一个唤醒;
notifyAll 唤醒monitor上所有block住的线程
4.2 Condition
await 在lock上block住
signal 在lock上随机释放一个线程
signalAll 唤醒lock上所有等待线程
总结: Object 的通知只能wait和notify在同一个队列里,Lock.newCondition可以分情况划分队列wait、notify.
http://tutorials.jenkov.com/java-concurrency/starvation-and-fairness.html
扩展阅读:LMAX Disruptor
https://itnext.io/understanding-the-lmax-disruptor-caaaa2721496