java concurrency 读书笔记

本文围绕Java并发展开,介绍了并发模型,如Parallel Workers和Assembly Line;阐述并发本质,包括Race Condition和Variable Visibility,以及阻塞和非阻塞处理方式;讲解CompareAndSwap常用类,如Lock、BlockingQueue、AtomicXxx;还介绍了Thread Signal的Object和Condition相关操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章地址: http://tutorials.jenkov.com/java-concurrency/index.html

Table of Contents

第一章 并发模型

第二章 并发的本质

第三章 CompareAndSwap常用类

第四章 Thread Signal


第一章 并发模型

 

1.1 Parallel Workers

Parallel Workers

1.2 Assembly Line

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值