死磕高并发与网络编程学习笔记及总结-并发包(三)

 

第三阶段

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、并发集合

BlockingQueueb1Boundedaddofferputremove
ArrayBlockingQueueBounded

1.成功return true

2.失败 抛出异常

1.成功return true

2.失败return false

如果队列已满,则会阻塞移除头部元素,如果为null则会抛出异常
PriorityBlockingQueueUnbounded同offer

成功返回true

绝不返回false

除非内存溢出

同offer移除头部元素,如果为null则会抛出异常
LinkedBlockingQueueOptional

1.成功return true

2.失败 抛出异常

1.成功return true

2.失败 抛出异常

如果队列已满,则会阻塞移除头部元素,如果为null则会抛出异常
LinkedBlockingDequeOptional同addLast

 

  
SynchronousQueueBounded

1.成功return true

2.失败 抛出异常

如果没有接受线程,返回false阻塞直到另外一个线程接受移除头部元素,如果为null则会抛出异常
DelayQueueUnbounded同offer,如果是false则抛异常

在尾部插入元素

弱国为null抛异常

同offer 
LinkedTransferQueueUnbounded

在尾部插入元素

永远返回true

同add同add移除头部元素,如果为null则会抛出异常
BlockingQueueb2pollelementpeektake
ArrayBlockingQueue

移除头部元素

并返回

获取头部元素

如果是null

抛出异常并不会删除

获取头部元素

并不会删除元素

如果队列为空则等待

否则移除头部元素

PriorityBlockingQueue

元素必须实现Comparable接口或者构造PriorityQueue时要传递Comparaor

移除头部元素

并返回

获取头部元素

如果是null

抛出异常并不会删除

获取头部元素

并不会删除元素

如果队列为空则等待

否则移除头部元素

LinkedBlockingQueue

移除头部元素

并返回

获取头部元素

如果是null

抛出异常并不会删除

获取头部元素

并不会删除元素

如果队列为空则等待

否则移除头部元素

LinkedBlockingDeque    
SynchronousQueue

移除头部元素并返回,如果元素可用不为空

否则为空

会永远爬出异常直接返回null

一直等待,有线程

transfer元素

DelayQueue

元素必须实现Delayed接口

如果灭有元素或者元素

没有过期都会返回null

获取头部元素

如果是null

抛出异常并不会删除

获取头部元素

并不会删除元素

一直等待,有线程放进元素并且头部元素过期
LinkedTransferQueue移除头部元素并返回

获取头部元素

如果是null

抛出异常并不会删除

获取头部元素

并不会删除元素

一直等待,有线程放进元素

 LinkedTransferQueue的transferfangf

在队列尾部插入元素。如果没有被消费则一直等待

     
HashMap没有加锁,线程非安全   
HashTable线程安全   
CurrentHashMap线程安全数组、列表、红黑树  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值