导图社区 图解多线程设计模式
图解多线程设计模式的思维导图。
编辑于2020-05-29 12:20:28图解多线程设计模式
java线程
Thread
线程的启动
extends Thread
implements Runnable
线程的暂停/唤醒
Thread.sleep()
Thread.interrupt()
线程的互斥
synchronized方法/代码块
线程的协作
wait
notify/notifyAll
线程的状态迁移
NEW,RUNNABLE,TERMINATED,WAITING,TIMED_WAITING,BLOCKED
其他
线程的取消
interrupt
interrupted
isInterrupted
InterruptedException
线程的优先级
setPriority
getPriority
等待线程终止
join
多线程的评价标准
必要条件
安全性--不损坏对象
生存性---必要的处理能够被执行
提高质量
可复用性--类可重复利用
性能-能快速 大批量地执行处理
Single Thread Execution 模式
手段: synchronized
使用场景
多个线程访问,状态有可能发生变化,需要确保安全性
生存性与死锁
可复用性与继承反常
临界区的大小和性能
一般会降低性能
获取锁花费时间
线程 冲突引起的等待
synchronized
before/After模式
保护着访问共享字段
保护的粒度
使用哪个锁保护
原子操作
信号量Semaphore
Immutable模式
手段
使用不可变对象
使用场景
实例创建后 状态不再发生变化
实例是共享的 且被频繁访问 时
性能
考虑成对的mutable和immutable类
string和stringbuffer
为了确保不可变性(可复用性)
标准库里的例子
String
BigInteger
包装类
final
类:无法扩展
方法:子类不能重写
字段:只能赋值一次
局部变量 和参数
集合类和多线程
Collections.synchronizedList
CopyOnWriteArrayList
Guarded Suspension 模式
实现方式
synchronized
while(xxx) wait ;
notify/ nofityAll
LinkedBlockckingQueue
take
put
Balking 模式
实现方式
synchronized
if(xxx) return
使用场景
并不需要执行时
不需要等待守护条件成立时
守护条件仅在第一次成立
超时
obj.wait(1000);
juc包超时处理方式
通过异常TimeOutException
Future.get
Exchager.exchange
Cyclicarrier.await
CountDownLatch.await
通过返回值
BlockingQueue
offer ->null
poll -> null
Semaphore
tryAcquire -> false
lock
tryLock -> false
Producer-Consume模式
实现方式
Data
Producer
Consumer
Channel
Data接收方式
队列
栈
优先队列
Channel角色的意义
线程的协调 运行要考虑 放在中间的东西
线程的互斥要考虑应该 保护的东西
InterruptedException异常
花费时间的方法
可以取消的方法
典型
Object.wait
Thread.sleep
Thread.join
如何发生
athread.interrupt()
interrupt方法只是改变类线程的中断状态。 sleep,wait,join会对线程的中断状态检查进而抛出异常
检查中断 状态
线程实例方法isInterrupted
线程静态方法 interrupted() 会清除中断状态,且操作对象只能时当前线程。
juc包和pc模式
BlockingQueue
ArrayBlockingQueue
LinkedBlockingQueue
PriorityBlockingQueue
DelayQueue
SynchronousQueue
ConcurrentLinkedQueue
Exchanager
Read-Write Lock 模式
实现方式
SharedResoure
ReadWriteLock
Writer
Reader
使用场景
适合读取操作繁重
利用读取操作的线程不会冲突的特性来提高程序性能
适合读取频率比写入频率高
juc.locks和Read-WriteLock 模式
ReentrantReadWriteLock
公平性
可重入性
锁降级
Thread-Per-Message 模式
实现方式
Client
Host
Helper
优势
提高响应性,缩短延迟时间
适用于操作顺序没有要求时
适用于不需要返回值时
应用于服务器
调用方法 + 启动线程-》发送消息
juc包和 T_P_M模式
THread
ThreadFactory
Runable
Executors
Executor
ExecutorService
ScheduledExecutorService
Worker Thread 模式
角色
Client
Channel
Worker
Request
优势
提高吞吐量
容量控制
调用与执行的分离
提高响应速度
控制执行顺序
可以取消和反复执行
通往分布式之路
juc包
ThreadPoolExecutor
Executors
newFixedThreadPool
newCachedThreadPool
newScheduledThreadPool
Feture模式
角色
Client
Host
VitualData
RealData
Feture
优势
提高吞吐量
异步方法调用的 返回值
准备返回值 和 使用返回值的分离
可复用性
juc包
Feture
FetureTaskk
Callable
Two-Phase Termination 模式
角色
TerminationRequester
Terminator
注意点
仅仅检查标志时不够的
仅仅检查中断状态时不够的
ExecutorService
中断状态 与InterryptedException的相互转换
juc包与线程同步
CountDownLatch
CyclicBarrier
Thread-Specific Storage模式
ThreadLocal
角色
OjectProxy
Client
TSObjectCollection
TSObject
优势
保存线程特有的信息的位置
不必担心其他线程的访问
吞吐量的提高取决于实现方式
上下文的危险性
Active Object 模式
角色
Client
ACtiveObject
Proxy
Scheduler
MethodRequest
ConcreteMethodRequest
Servant
ActivationQueue
Feture
RealResult
juc包与 Active Object模式
Executors
ExecutoreService
Callable
Runnable
Feture