3.4.3. HashTable
(线程安全)
........................................................................................................... 53
3.4.4. TreeMap
(可排序)
.................................................................................................................. 53
3.4.5. LinkHashMap
(记录插入顺序)
.............................................................................................. 53
4. JAVA 多线程并发 ................................................................................................................................. 54
4.1.1. JAVA
并发知识库
....................................................................................................................... 54
4.1.2. JAVA
线程实现
/
创建方式
.......................................................................................................... 54
4.1.2.1. 继承 Thread 类 ........................................................................................................................................ 54
4.1.2.2. 实现 Runnable 接口。 ............................................................................................................................ 54
4.1.2.3. ExecutorService、Callable<Class>、Future 有返回值线程 ............................................................. 55
4.1.2.4. 基于线程池的方式 ................................................................................................................................... 56
4.1.3. 4
种线程池
................................................................................................................................... 56
4.1.3.1. newCachedThreadPool ......................................................................................................................... 57
4.1.3.2. newFixedThreadPool ............................................................................................................................. 57
4.1.3.3. newScheduledThreadPool .................................................................................................................... 58
4.1.3.4. newSingleThreadExecutor ................................................................................................................. 58
4.1.4.
线程生命周期
(
状态
) .................................................................................................................... 58
4.1.4.1. 新建状态(NEW) ................................................................................................................................. 58
4.1.4.2. 就绪状态(RUNNABLE): ................................................................................................................. 59
4.1.4.3. 运行状态(RUNNING): .................................................................................................................... 59
4.1.4.4. 阻塞状态(BLOCKED): .................................................................................................................... 59
等待阻塞(o.wait->等待对列): ...................................................................................................................... 59
同步阻塞(lock->锁池) .......................................................................................................................................... 59
其他阻塞(sleep/join) ............................................................................................................................................ 59
4.1.4.5. 线程死亡(DEAD) ................................................................................................................................ 59
正常结束 ................................................................................................................................................................ 59
异常结束 ................................................................................................................................................................ 59
调用 stop ............................................................................................................................................................... 59
4.1.5.
终止线程
4
种方式
...................................................................................................................... 60
4.1.5.1. 正常运行结束 ........................................................................................................................................... 60
4.1.5.2. 使用退出标志退出线程 ........................................................................................................................... 60
4.1.5.3. Interrupt 方法结束线程 ........................................................................................................................... 60
4.1.5.4. stop 方法终止线程(线程不安全) ....................................................................................................... 61
4.1.6. sleep
与
wait
区别
....................................................................................................................... 61
4.1.7. start
与
run
区别
.......................................................................................................................... 62
4.1.8. JAVA
后台线程
........................................................................................................................... 62
4.1.9. JAVA
锁
....................................................................................................................................... 63
4.1.9.1. 乐观锁 ...................................................................................................................................................... 63
4.1.9.2. 悲观锁 ...................................................................................................................................................... 63
4.1.9.3. 自旋锁 ...................................................................................................................................................... 63
自旋锁的优缺点 .................................................................................................................................................... 63
自旋锁时间阈值(1.6 引入了适应性自旋锁) .................................................................................................. 63
自旋锁的开启 ........................................................................................................................................................ 64
4.1.9.4. Synchronized 同步锁 .............................................................................................................................. 64
Synchronized 作用范围 ....................................................................................................................................... 64
Synchronized 核心组件 ....................................................................................................................................... 64
Synchronized 实现 ............................................................................................................................................... 64
4.1.9.5. ReentrantLock ......................................................................................................................................... 66
Lock 接口的主要方法 ........................................................................................................................................... 66
非公平锁 ................................................................................................................................................................ 66
公平锁 .................................................................................................................................................................... 67
ReentrantLock 与 synchronized ........................................................................................................................ 67
ReentrantLock 实现 ............................................................................................................................................. 67
Condition 类和 Object 类锁方法区别区别 ......................................................................................................... 68
tryLock 和 lock 和 lockInterruptibly 的区别 ........................................................................................................ 68
4.1.9.6. Semaphore 信号量 ................................................................................................................................. 68
实现互斥锁(计数器为 1) ................................................................................................................................. 68
代码实现 ................................................................................................................................................................ 68
Semaphore 与 ReentrantLock ........................................................................................................................... 69
4.1.9.7. AtomicInteger .......................................................................................................................................... 69
评论0