第三阶段
1、原子类型详细讲解
AtomicInteger(compareAndSet 典型的cas对比算法)
释放锁的时候要考虑到多线程的情况,只有当前线程才可以关闭当前线程
1、可见性
2、有顺性
3、原子性
1、volatile修饰的变量,能保证前俩者
2、CAS算法,也就是CPU级别的同步指令,相当于乐观锁,它可以探测到其他线程对共享数据的变化情况
atomicInteger++
incrementAndGet()
for(;;){
int current = get();
int next = current +1;
if(compareAndSet(current,next))
return next;
}
最快失败策略
CAS轻量级锁,带来的一个严重问题,ABA问题
T1 T2
A A->B->A
链表,栈,后进先出
2、并发包工具
CountDwonLatch
3、Executors框架详细介绍
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters and default thread factory.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), handler);
}
4、并发集合
BlockingQueueb1 | Bounded | add | offer | put | remove |
ArrayBlockingQueue | Bounded | 1.成功return true 2.失败 抛出异常 | 1.成功return true 2.失败return false | 如果队列已满,则会阻塞 | 移除头部元素,如果为null则会抛出异常 |
PriorityBlockingQueue | Unbounded | 同offer | 成功返回true 绝不返回false 除非内存溢出 | 同offer | 移除头部元素,如果为null则会抛出异常 |
LinkedBlockingQueue | Optional | 1.成功return true 2.失败 抛出异常 | 1.成功return true 2.失败 抛出异常 | 如果队列已满,则会阻塞 | 移除头部元素,如果为null则会抛出异常 |
LinkedBlockingDeque | Optional | 同addLast |
| ||
SynchronousQueue | Bounded | 1.成功return true 2.失败 抛出异常 | 如果没有接受线程,返回false | 阻塞直到另外一个线程接受 | 移除头部元素,如果为null则会抛出异常 |
DelayQueue | Unbounded | 同offer,如果是false则抛异常 | 在尾部插入元素 弱国为null抛异常 | 同offer | |
LinkedTransferQueue | Unbounded | 在尾部插入元素 永远返回true | 同add | 同add | 移除头部元素,如果为null则会抛出异常 |
BlockingQueueb2 | poll | element | peek | take |
ArrayBlockingQueue | 移除头部元素 并返回 | 获取头部元素 如果是null 抛出异常并不会删除 | 获取头部元素 并不会删除元素 | 如果队列为空则等待 否则移除头部元素 |
PriorityBlockingQueue 元素必须实现Comparable接口或者构造PriorityQueue时要传递Comparaor | 移除头部元素 并返回 | 获取头部元素 如果是null 抛出异常并不会删除 | 获取头部元素 并不会删除元素 | 如果队列为空则等待 否则移除头部元素 |
LinkedBlockingQueue | 移除头部元素 并返回 | 获取头部元素 如果是null 抛出异常并不会删除 | 获取头部元素 并不会删除元素 | 如果队列为空则等待 否则移除头部元素 |
LinkedBlockingDeque | ||||
SynchronousQueue | 移除头部元素并返回,如果元素可用不为空 否则为空 | 会永远爬出异常 | 直接返回null | 一直等待,有线程 transfer元素 |
DelayQueue 元素必须实现Delayed接口 | 如果灭有元素或者元素 没有过期都会返回null | 获取头部元素 如果是null 抛出异常并不会删除 | 获取头部元素 并不会删除元素 | 一直等待,有线程放进元素并且头部元素过期 |
LinkedTransferQueue | 移除头部元素并返回 | 获取头部元素 如果是null 抛出异常并不会删除 | 获取头部元素 并不会删除元素 | 一直等待,有线程放进元素 |
LinkedTransferQueue的transferfangf
在队列尾部插入元素。如果没有被消费则一直等待
HashMap | 没有加锁,线程非安全 | |||
HashTable | 线程安全 | |||
CurrentHashMap | 线程安全 | 数组、列表、红黑树 |