《java并发编程实战》

(第5章的ConcurrentHashMap部分未完成)

第2章 线程安全性

无状态对象一定是线程安全的,无状态:既不包含任何域,也不包含对其他类中域的引用。

竞态条件(Race Condition):因不恰当的执行时序而出现不正确的结果(要 想获得正确的结果,取决于事件发生的时序)

原子性:i++不是原子的,包括read-update-write。

对于无状态的类:

增加单个域,可以简单将其包装为原子的,保证对该域的操作具有原子性,例如java.util.concurrent.atomic中的类。

如果增加多个域,且多个域之间是相关的,简单的分别包装为原子的并不能保证线程安全,因为两个域之间的操作不具有原子性。(可以将相关域包装为一个不可变对象使其具有原子性)

synchronized(可重入):

        1.非静态方法:以this作为锁

        2.静态方法:以Class对象作为锁

        3.同步代码块:任意对象
                   可重入示例:

        public class Parent {
            public synchronized void doSomething() {
                ...
            }
        }

        public class Child extends Parent {
            //child.doSomething时锁住的是子类对象,super.doSomething锁住的也是子类对象.
            @Override
            public synchronized void doSomething() {
                 ...
                super.doSomething();
            }
        }

第3章 对象的共享

64位的读操作或写操作分解为两个32位的操作,读取一个非volatile类型的long或double变量时,如果读写操作在不同的线程中执行,可能会读到某个值的高32位和另一个值的低32位(在多线程中使用共享且可变的long和double类型变量时,需使用volatile)

不要轻易使用volatile,当且仅当满足以下所有条件时,才该使用:

    1. 对变量的写入不依赖变量的当前值, 或者确保只有单个线程更新变量的值.(因为volatile不保证原子性)

    2. 该变量不会与其他状态变量一起纳入不变性条件中. 

    3. 在访问变量时不需要加锁.(加锁可以保证volatile保证的可见性,不需要使用volatile)

发布与逸出(publicsh&escape)

发布一个对象,是指使该对象在作用域之外的代码中使用,如将对象引用保存到其他代码可以访问的地方,或在某个非私有方法中返回该引用,或将对象引用传递到其他的类的方法中。

逸出是指某个不该发布的对象被发布。

不要在构造函数中使this逸出(例如在构造函数中启动一个线程,这会导致在对象为完全构造之前,新的线程就会看见它。或调用一个非private非final方法,都会使this逸出,可以使用私有构造方法和public的工厂方法来避免上述构造过程)

线程封闭:

    1.ad-hoc封闭:维护线程封闭性的职责完全由程序实现来承担。

    2.栈封闭:局部变量

    3.ThreadLocal

不变性:不可变对象一定是线程安全的。
        

安全发布一个对象: ??????????????????????????

    1.在静态初始化函数中初始化一个对象引用。

    2.将对象的引用保存到volatile类型的域或者AtomicReferance对象中。

    3.将对象引用保存到某个正确构造对象的final类型域中。

    4.将对象的引用保存到一个由锁保护的域中。


第4章 对象的组合

线程安全性的委托:

对于组合对象,如果各个组件都是线程安全的,且组件之间相互独立,那么组合对象是线程安全的(即线程安全可以委托给组件);如果组件之间存在某些相互制约的条件,那么组合对象不是线程安全的。
               非线程安全的组合类实例:

//区间类,由于下界必须小于等于上界,但是两个set方法都是“先检查-后执行”,却没有相应的加锁机制,会出错。          
public class NumberRange{
    private final AtomicInteger lower=new AtomicInteger(0);
    private final AtomicInteger upper=new AtomicInteger(0);

    public void setLower(int i){
        if(i>upper.get()){
            throw...
        }
        lower.set(i);
    }
                       
    public void setUpper(int i){
        if(i<lower.get()){
            throw...
        }
        upper.set(i);
    }
}

在现有线程安全类中添加功能:

    1.继承现有安全类

    2.客户端加锁(客户端是使用现有线程安全类的部分),注意要使用与现有线程安全类相同的锁。(前两种耦合性高,破坏实现封装性)

    3.组合   

//在List中添加“没有则添加”功能
//ImprovedList中通过内置锁缝状List的操作,所有的操作不使用基本List,而是使用ImprovedList。
public class ImprovedList<T> implements List<T> {
    private final List<T> list;
    public ImprovedList(List<T> list) {
        this.list = list;
    }

    public synchronized boolean putIfAbsent(T x) {
        boolean contains = list.contains(x);
        if (contains){
            list.add(x);
        }
        return !contains;
    }

    public synchronized void xxx(){
        list.xxx()么,
    }
    //...
}


第5章 基础构建模块

ConcurrentModificationException

ConcurrentHashMap

//内部类
static class Node<K,V> implements Map.Entry<K,V>{
    ...//只读
}   


/**
*-1表示初始化;-(1+n)表示有n个线程在resize;0表示为初始化;正数表示table的容量。
*/
private transient volatile int sizeCtl;

/**
*resize或create CounterCell时的自旋锁。
*/
private transient volatile int cellsBusy;

/**
*counterCell 数组
*/
private transient volatile CounterCell[] counterCells;

//初始化table:
private final Node<K,V>[] initTable() {
    Node<K,V>[] tab; int sc;
    while ((tab = table) == null || tab.length == 0) {
        //如果sizeCtl小于0,那么yield(),只允许一个线程对table进行初始化。
        if ((sc = sizeCtl) < 0)
            Thread.yield(); // lost initialization race; just spin
        else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {//通过cas设置sizeCtl的值
            try {
                if ((tab = table) == null || tab.length == 0) {
                    int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
                    @SuppressWarnings("unchecked")
                    Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
                    table = tab = nt;
                    sc = n - (n >>> 2);
                }
            } finally {
                sizeCtl = sc;
            }
            break;
        }
    }
    return tab;
}

put()

    //put
    public V put(K key, V value) {
        //onlyIfAbsent=false;意味着不缺失也插入
        return putVal(key, value, false);
    }
    /** Implementation for put and putIfAbsent */
    final V putVal(K key, V value, boolean onlyIfAbsent) {
        if (key == null || value == null) throw new NullPointerException();
        //spread()对hash值做处理
        int hash = spread(key.hashCode());
        int binCount = 0;
        //死循环,插入成功再跳出
        for (Node<K,V>[] tab = table;;) {
            Node<K,V> f; int n, i, fh;
            //延迟初始化,第一次插入节点的时候才会初始化
            if (tab == null || (n = tab.length) == 0)
                tab = initTable();
            //tabAt()根据hash值获得在table中该位置的节点f,如果f==null,利用cas插入。
            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
                if (casTabAt(tab, i, null,
                             new Node<K,V>(hash, key, value, null)))
                    break;                   // no lock when adding to empty bin
            }
            //根据hash获得的table中该位置节点的hash==MOVED,这说明
            //该节点为ForwardingNode,说明该节点正处于transfer过程中,所以helpTransfer()
            else if ((fh = f.hash) == MOVED)
                tab = helpTransfer(tab, f);
            //根据hash获得的table中该位置节点f!=null,且也不是ForwardingNode,
            //说明发生冲突,利用synchronized获得锁,锁是该节点f,进行插入。
            else {
                V oldVal = null;
                synchronized (f) {
                    if (tabAt(tab, i) == f) {//理解是可能在插入过程中该位置节点已经被其他线程插入过了,所以这里判断一下(不知道理解的对不对)
                        if (fh >= 0) {//fh就是该位置节点f的hash值,>0说明是链表头节点,按照链表插入进行
                            binCount = 1;
                            for (Node<K,V> e = f;; ++binCount) {
                                K ek;
                                if (e.hash == hash &&
                                    ((ek = e.key) == key ||
                                     (ek != null && key.equals(ek)))) {
                                    oldVal = e.val;
                                    if (!onlyIfAbsent)
                                        e.val = value;
                                    break;
                                }
                                Node<K,V> pred = e;
                                if ((e = e.next) == null) {
                                    pred.next = new Node<K,V>(hash, key,
                                                              value, null);
                                    break;
                                }
                            }
                        }
                        else if (f instanceof TreeBin) {//这说明是树的跟节点,按照插入树节点进行插入
                            Node<K,V> p;
                            binCount = 2;
                            if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
                                                           value)) != null) {
                                oldVal = p.val;
                                if (!onlyIfAbsent)
                                    p.val = value;
                            }
                        }
                    }
                }
                //binCount!=0说明进行了链表或树插入,判断是否需要进行treeifyBin(),treeifyBin()可能把链表转换为树,也可能把table扩容。
                if (binCount != 0) {
                    if (binCount >= TREEIFY_THRESHOLD)
                        treeifyBin(tab, i);
                    if (oldVal != null)
                        return oldVal;
                    break;
                }
            }
        }
        //
        addCount(1L, binCount);
        return null;
    }

addCount()

    private final void addCount(long x, int check) {
        CounterCell[] as; long b, s;
        if ((as = counterCells) != null ||
            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
            CounterCell a; long v; int m;
            boolean uncontended = true;
            if (as == null || (m = as.length - 1) < 0 ||
                (a = as[ThreadLocalRandom.getProbe() & m]) == null ||
                !(uncontended =
                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
                fullAddCount(x, uncontended);
                return;
            }
            if (check <= 1)
                return;
            s = sumCount();
        }
        if (check >= 0) {
            Node<K,V>[] tab, nt; int n, sc;
            //s(table中Node个数)大于sc=sizeCtl时且table!=null且table.length不超过最大长度,可以扩容
            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
                   (n = tab.length) < MAXIMUM_CAPACITY) {
                //进行扩容,先通过resizeStamp(n)设置扩容时的标记位,在扩容时是sizeCtl的高16位。
                int rs = resizeStamp(n);
                //sc<0,表示目前有其他线程在扩容
                if (sc < 0) {
                    // 这块的几个条件,不是很理解,大致如下
                    //(sc >>> RESIZE_STAMP_SHIFT) != rs:sc先右移16位,这时候也就是得到了标记位,!=rs
                    //sc == rs + 1 
                    //sc == rs + MAX_RESIZERS : 同时扩容线程数达到最大
                    //(nt = nextTable) == null : 新表==null
                    //transferIndex <=0 : transferIndex<=0表示该线程不需要帮助扩容,这个变量表示某线程负责扩容table区间的边界。
                    if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
                        sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
                        transferIndex <= 0)
                        break;
                    //CAS设置sizeCtl+1,多一个线程帮助扩容
                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
                        transfer(tab, nt);
                }
                //第一个进行扩容的线程,利用cas设置sizeCtl的值,高16位是标记位,低16位是2(扩容线程数+1)
                else if (U.compareAndSwapInt(this, SIZECTL, sc,
                                             (rs << RESIZE_STAMP_SHIFT) + 2))
                    transfer(tab, null);
                s = sumCount();
            }
        }
    }

    /**
     * Returns the stamp bits for resizing a table of size n.
     * Must be negative when shifted left by RESIZE_STAMP_SHIFT.
     * 扩容大小为n的table时的标记位,该数左移(RESIZE_STAMP_SHIFT=16)之后一定是个负数。
     */
    static final int resizeStamp(int n) {
        return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1));
    }

transfer

    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
        int n = tab.length, stride;
        //stride负责扩容的table区间范围
        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
            stride = MIN_TRANSFER_STRIDE; // subdivide range
        //初始化nextTable
        if (nextTab == null) {            // initiating
            try {
                @SuppressWarnings("unchecked")
                Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
                nextTab = nt;
            } catch (Throwable ex) {      // try to cope with OOME
                sizeCtl = Integer.MAX_VALUE;
                return;
            }
            nextTable = nextTab;
            transferIndex = n;
        }
        int nextn = nextTab.length;
        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
        boolean advance = true;
        boolean finishing = false; // to ensure sweep before committing nextTab
        for (int i = 0, bound = 0;;) {
            Node<K,V> f; int fh;
            while (advance) {
                int nextIndex, nextBound;
                if (--i >= bound || finishing)
                    advance = false;
                else if ((nextIndex = transferIndex) <= 0) {
                    i = -1;
                    advance = false;
                }
                else if (U.compareAndSwapInt
                         (this, TRANSFERINDEX, nextIndex,
                          nextBound = (nextIndex > stride ?
                                       nextIndex - stride : 0))) {
                    bound = nextBound;
                    i = nextIndex - 1;
                    advance = false;
                }
            }
            if (i < 0 || i >= n || i + n >= nextn) {
                int sc;
                if (finishing) {
                    nextTable = null;
                    table = nextTab;
                    sizeCtl = (n << 1) - (n >>> 1);
                    return;
                }
                if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
                    if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
                        return;
                    finishing = advance = true;
                    i = n; // recheck before commit
                }
            }
            else if ((f = tabAt(tab, i)) == null)
                advance = casTabAt(tab, i, null, fwd);
            else if ((fh = f.hash) == MOVED)
                advance = true; // already processed
            else {
                synchronized (f) {
                    if (tabAt(tab, i) == f) {
                        Node<K,V> ln, hn;
                        if (fh >= 0) {
                            int runBit = fh & n;
                            Node<K,V> lastRun = f;
                            for (Node<K,V> p = f.next; p != null; p = p.next) {
                                int b = p.hash & n;
                                if (b != runBit) {
                                    runBit = b;
                                    lastRun = p;
                                }
                            }
                            if (runBit == 0) {
                                ln = lastRun;
                                hn = null;
                            }
                            else {
                                hn = lastRun;
                                ln = null;
                            }
                            for (Node<K,V> p = f; p != lastRun; p = p.next) {
                                int ph = p.hash; K pk = p.key; V pv = p.val;
                                if ((ph & n) == 0)
                                    ln = new Node<K,V>(ph, pk, pv, ln);
                                else
                                    hn = new Node<K,V>(ph, pk, pv, hn);
                            }
                            setTabAt(nextTab, i, ln);
                            setTabAt(nextTab, i + n, hn);
                            setTabAt(tab, i, fwd);
                            advance = true;
                        }
                        else if (f instanceof TreeBin) {
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
                            TreeNode<K,V> lo = null, loTail = null;
                            TreeNode<K,V> hi = null, hiTail = null;
                            int lc = 0, hc = 0;
                            for (Node<K,V> e = t.first; e != null; e = e.next) {
                                int h = e.hash;
                                TreeNode<K,V> p = new TreeNode<K,V>
                                    (h, e.key, e.val, null, null);
                                if ((h & n) == 0) {
                                    if ((p.prev = loTail) == null)
                                        lo = p;
                                    else
                                        loTail.next = p;
                                    loTail = p;
                                    ++lc;
                                }
                                else {
                                    if ((p.prev = hiTail) == null)
                                        hi = p;
                                    else
                                        hiTail.next = p;
                                    hiTail = p;
                                    ++hc;
                                }
                            }
                            ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
                                (hc != 0) ? new TreeBin<K,V>(lo) : t;
                            hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
                                (lc != 0) ? new TreeBin<K,V>(hi) : t;
                            setTabAt(nextTab, i, ln);
                            setTabAt(nextTab, i + n, hn);
                            setTabAt(tab, i, fwd);
                            advance = true;
                        }
                    }
                }
            }
        }
    }

helpTransfer()

https://sylvanassun.github.io/2018/03/16/2018-03-16-map_family

同步工具类

java中的同步工具类都是根据AQS(AbstractQueuedSynchronizer)实现的。

AQS介绍见水岩的博客:https://www.cnblogs.com/waterystone/p/4920797.html

我也有转载:https://blog.csdn.net/u013264046/article/details/89523308

Semaphore

看过AQS介绍之后,看一下Semaphore中那不勒Sync的部分源码,

    abstract static class Sync extends AbstractQueuedSynchronizer {
        ...
        //state保存当前可用许可的数量。

        //非公平的tryAcquireShared
        final int nonfairTryAcquireShared(int acquires) {
            for (;;) {
                //当前可用的许可数量
                int available = getState();
                //计算分配acquires之后剩余的许可数量
                int remaining = available - acquires;
                //如果剩余的数量小于0,那么直接返回一个负数,表示失败。否则cas操作设置state值为剩余数量,并返回剩余数量(一个非负数,表示成功)
                if (remaining < 0 ||
                    compareAndSetState(available, remaining))
                    return remaining;
            }
        }
        
        //尝试释放资源,tryReleaseShared
        protected final boolean tryReleaseShared(int releases) {
            for (;;) {
                int current = getState();
                int next = current + releases;
                if (next < current) // overflow
                    throw new Error("Maximum permit count exceeded");
                if (compareAndSetState(current, next))
                    return true;
            }
        }
    }

    /**
     * NonFair version
     */
    static final class NonfairSync extends Sync {
        private static final long serialVersionUID = -2694183684443567898L;

        NonfairSync(int permits) {
            super(permits);
        }

        protected int tryAcquireShared(int acquires) {
            return nonfairTryAcquireShared(acquires);
        }
    }

    /**
     * Fair version
     */
    static final class FairSync extends Sync {
        private static final long serialVersionUID = 2014338818796000944L;

        FairSync(int permits) {
            super(permits);
        }
        
        //公平的tryAcquireShared
        protected int tryAcquireShared(int acquires) {
            for (;;) {
                //这里判断当前线程之前是否有在等待的线程(AQS中的方法),如果有,由于是公平的,直接返回-1,表示失败。
                if (hasQueuedPredecessors())
                    return -1;
                int available = getState();
                int remaining = available - acquires;
                if (remaining < 0 ||
                    compareAndSetState(available, remaining))
                    return remaining;
            }
        }
    }

CountDownLatch

CountDownLatch内部类Sync的部分源码:

    private static final class Sync extends AbstractQueuedSynchronizer {
        ...
        //state 用来保存计数。
        
        //当计数为0时,返回1(成功),否则返回-1(失败)
        protected int tryAcquireShared(int acquires) {
            return (getState() == 0) ? 1 : -1;
        }
        
        //当前计数减去释放的数量,剩余为0时,才返回true
        protected boolean tryReleaseShared(int releases) {
            // Decrement count; signal when transition to zero
            for (;;) {
                int c = getState();
                if (c == 0)
                    return false;
                int nextc = c-1;
                if (compareAndSetState(c, nextc))
                    return nextc == 0;
            }
        }
    }

CountDownLatch中的await方法和countDown方法如下:

    public void await() throws InterruptedException {
        sync.acquireSharedInterruptibly(1);
    }

    public void countDown() {
        sync.releaseShared(1);
    }

await调用acquireSharedInterruptibly(1),就是acquireShared的可中断方式,其中会调用tryAcquireShared(1),如果当前state==0,那么tryAcquireShared会返回1,acquireSharedInterruptibly方法中不会使当前线程不会进入等待队列(AQS的实现方式),直接返回。如果state!=0,那么当前线程进入等待队列。

countDown调用releaseShared(1),其中会调用tryReleaseShared(1),只有当state在释放后变为0时,tryReleaseShared方法才会返回true,从而releaseShared方法才会使得所有await的线程继续。

ReentrantLock

ReentrantLock和前面两种不同,其只支持独占锁,所以内部类Sync实现的是tryAcquire和tryRelease方法。

内部类Sync的部分源码:

    abstract static class Sync extends AbstractQueuedSynchronizer {

        abstract void lock();

        /**
         * 非公平的tryAcquire
         */
        final boolean nonfairTryAcquire(int acquires) {
            final Thread current = Thread.currentThread();
            int c = getState();
            //state==0,尝试cas设置state。
            if (c == 0) {
                //如果cas设置成功,那么返回true,并设置ExclusiveOwnerOwnerThread是当前线程。(判断是否可重入时用到)
                if (compareAndSetState(0, acquires)) {
                    setExclusiveOwnerThread(current);
                    return true;
                }
            }
            //state!=0,表示当前锁被占用,判断该线程是否是当前线程,如果是,可重入,设置state
            else if (current == getExclusiveOwnerThread()) {
                int nextc = c + acquires;
                if (nextc < 0) // overflow
                    throw new Error("Maximum lock count exceeded");
                setState(nextc);
                return true;
            }
            return false;
        }
        
        //如果当前线程不是exclusiveOwnerThread,那么不能释放锁。只有当c==0时(由于可重入)才表示锁释放,设置exclusiveOwnerThread=null。
        protected final boolean tryRelease(int releases) {
            int c = getState() - releases;
            if (Thread.currentThread() != getExclusiveOwnerThread())
                throw new IllegalMonitorStateException();
            boolean free = false;
            if (c == 0) {
                free = true;
                setExclusiveOwnerThread(null);
            }
            setState(c);
            return free;
        }

        protected final boolean isHeldExclusively() {
            // While we must in general read state before owner,
            // we don't need to do so to check if current thread is owner
            return getExclusiveOwnerThread() == Thread.currentThread();
        }
        ...
    }

    /**
     * 非公平锁
     */
    static final class NonfairSync extends Sync {
        /**
         *非公平锁在lock时,立即尝试一次cas,若果成功,就设置exclusiveOwnerThread,否则再调用acquire,执行AQS中介绍的一系列操作。
         */
        final void lock() {
            if (compareAndSetState(0, 1))
                setExclusiveOwnerThread(Thread.currentThread());
            else
                acquire(1);
        }
        //非公平锁的tryAcquire方法,在acquire方法中会调用。
        protected final boolean tryAcquire(int acquires) {
            return nonfairTryAcquire(acquires);
        }
    }

    /**
     * 公平锁
     */
    static final class FairSync extends Sync {
        
        //公平锁在lock时,直接调用acquire。
        final void lock() {
            acquire(1);
        }

        /**
         *这是公平锁的tryAcquire,和非公平锁只有一处区别。
         */
        protected final boolean tryAcquire(int acquires) {
            final Thread current = Thread.currentThread();
            int c = getState();
            if (c == 0) {
                //和非公平锁的区别,这里要判断当前线程的前面,是否有其他线程在等待锁
                if (!hasQueuedPredecessors() &&
                    compareAndSetState(0, acquires)) {
                    setExclusiveOwnerThread(current);
                    return true;
                }
            }
            else if (current == getExclusiveOwnerThread()) {
                int nextc = c + acquires;
                if (nextc < 0)
                    throw new Error("Maximum lock count exceeded");
                setState(nextc);
                return true;
            }
            return false;
        }
    }

FutureTask

FutureTask并没有实现AQS。

FutureTask的可能状态如下,COMPLETING,INTERRUPTING称为中间状态,NORMAL,EXCEPTIONAL,CANCELLED,INTERRUPTED称为最终状态,能进入最终状态只有set、setException、cancel三个方法可以。

     /**
     * Possible state transitions:
     * NEW -> COMPLETING -> NORMAL
     * NEW -> COMPLETING -> EXCEPTIONAL
     * NEW -> CANCELLED
     * NEW -> INTERRUPTING -> INTERRUPTED
     */
    private volatile int state;
    private static final int NEW          = 0;
    private static final int COMPLETING   = 1;
    private static final int NORMAL       = 2;
    private static final int EXCEPTIONAL  = 3;
    private static final int CANCELLED    = 4;
    private static final int INTERRUPTING = 5;
    private static final int INTERRUPTED  = 6;
    public V get() throws InterruptedException, ExecutionException {
        int s = state;//状态
        if (s <= COMPLETING)
            s = awaitDone(false, 0L);
        return report(s);
    }

FutureTask.get()行为取决于状态state,如果任务已经完成,那么直接返回结果,否则get阻塞,直到任务进入完成状态,返回结果或者抛出异常。

第6章 任务执行

Executor框架

Executor接口

    public interface Executor {
        void execute(Runnable command);
    }

线程池

线程饥饿死锁:在线程池中,如果任务依赖于其他任务,那么可能产生死锁,只要线程池中的任务需要无限期等待一些必须由池中其他任务才能提供的资源或条件,那么除非线程池足够大,否则将发生线程饥饿死锁。

配置ThreadPoolExecutor:

    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler){...} 

corePoolSize——线程池基本大小,maximumPoolSize——线程池最大大小。当没有任务执行时,线程池中线程数量为corePoolSize,当workQueue满了之后,才会继续创建线程,直到达到maximumPoolSize。如果空闲线程超过了KeepaliveTime,那么该线程会被标记为可回收,并在线程池大小超过corePoolSize时,被终止。

workQueue是一个BlockingQueue,来保存等待执行的任务,分为无界队列、有界队列、同步移交(Synchronous Handoff).无界队列,如果所有工作者线程都处于忙碌状态,那么任务在队列中等待,可能会无限增加。有界队列的大小通常和线程池大小一起调节,有界队列满了之后根据饱和策略来处理。同步移交不是真正的队列,要将一个任务放入SynchronousQueue中,必须有另一个线程在等待接收这个任务。如果没有线程在等待,而且线程池当前大小小于最大值,那么就创造一个新的线程,如果线程池大小已经最大来,那么任务会被拒绝。

只有任务相互对立时,为线程池和工作队列设置界限才是合理的。如果任务之间互相依赖,有界可能会导致饥饿死锁。

饱和策略在有界队列满时发挥作用。AbortPolicy,默认的策略,抛出RejectedExecutionException。DiscardPolicy悄悄抛弃任务。DiscardOldestPolicy,抛弃最旧的(非优先级队列,抛弃下一个将被执行的任务,优先级队列,抛弃优先级最高的任务)。CallerRunsPolicy,在调用者线程中执行任务。

threadFactory,创建新线程的线程工厂。

    public interface ThreadFactory {
        Thread newThread(Runnable r);
    }

 扩展ThreadPoolExecutor,它提供了几个可以改写的方法:beforeExectue、afterExecute、terminated。无论任务是正常返回还是抛出一个异常返回,afterExecute都会被调用。(如果任务完成之后有Error,那么不会调用afterExecute。)如果beforeExecute抛出一个RuntimeExeception,那么任务不会执行,afterExecute也不会被调用。terminated在线程池完成关闭操作时调用,可以用来释放Executor在其生命周期里分配的各种资源,此外还可以执行发送通知、记录日志或手机finalize统计信息等。

第7章 取消和关闭

任务取消

Java中只能通过协作式的机制来停止线程、停止任务。

一种协作机制通过设置某个标志,任务定期查看标志,根据标志作出处理。

/**
 * PrimeGenerator
 * <p/>
 * Using a volatile field to hold cancellation state
 *
 * @author Brian Goetz and Tim Peierls
 */

public class PrimeGenerator implements Runnable {
    private static ExecutorService exec = Executors.newCachedThreadPool();

    private final List<BigInteger> primes = new ArrayList<BigInteger>();
    private volatile boolean cancelled;

    public void run() {
        BigInteger p = BigInteger.ONE;
        while (!cancelled) {
            p = p.nextProbablePrime();
            synchronized (this) {
                primes.add(p);
            }
        }
    }

    public void cancel() {
        cancelled = true;
    }

    public synchronized List<BigInteger> get() {
        return new ArrayList<BigInteger>(primes);
    }

    static List<BigInteger> aSecondOfPrimes() throws InterruptedException {
        PrimeGenerator generator = new PrimeGenerator();
        new Thread(generator).start();
        try {
            SECONDS.sleep(1);
        } finally {
            generator.cancel();
        }
        return generator.get();
    }
}

中断           

每个线程拥有自己的中断策略,除非知道中断对该线程的意义,否则不应该中断该线程。

    public class Thread{
        public void interrupt(){...}//(1)
        public boolean isInterrupted(){...}//(2)
        public static boolean interrupted(){...}//(3)
        ...
    }

(1)interrupt()方法能中断目标线程(中断并不是真正中断一个正在运行的线程,而只是发出中断请求(设置中断状态),由线程在下一个合适的时刻中断自己。例如某些方法wait、sleep、join等,如果发现中断状态被设置,会清除中断状态,并抛出InterruptedException。

(2)isInterrupted()方法返回目标线程的中断状态。

(3)interrupted()方法会清除当前线程的中断状态,返回它之前的值(true or false),返回true时,应该对该中断进行处理,可以继续抛出InterruptedException,或者调用interrupt()方法重新设置中断状态。

中断是实现取消的最合理方式。

/**
 * PrimeProducer
 * <p/>
 * Using interruption for cancellation
 *
 * @author Brian Goetz and Tim Peierls
 */
public class PrimeProducer extends Thread {
    private final BlockingQueue<BigInteger> queue;

    PrimeProducer(BlockingQueue<BigInteger> queue) {
        this.queue = queue;
    }

    public void run() {
        try {
            BigInteger p = BigInteger.ONE;
            while (!Thread.currentThread().isInterrupted())
                queue.put(p = p.nextProbablePrime());
        } catch (InterruptedException consumed) {
            /* Allow thread to exit */
        }
    }

    public void cancel() {
        interrupt();
    }
}

响应中断

两种策略:

    1.传递异常

    2.恢复中断状态(调用interrupted()方法)

对于不支持取消,但可以调用可中断的阻塞方法的任务,必须在循环中调用方法,并在发现中断后重新尝试。这种情况下,应该在本地保存中断状态,并在任务返回时恢复中断状态,而不是在捕获InterruptedException时恢复。例如:

/**
 * NoncancelableTask
 * <p/>
 * Noncancelable task that restores interruption before exit
 *
 * @author Brian Goetz and Tim Peierls
 */
public class NoncancelableTask {
    public Task getNextTask(BlockingQueue<Task> queue) {
        boolean interrupted = false; //本地保存中断状态
        try {
            while (true) {
                try {
                    return queue.take();
                } catch (InterruptedException e) {
                    interrupted = true;
                    // 重新尝试
                    // 如果在这里恢复中断状态,由于queue.take()会在入口出检查中断状态,发现位true时会立即抛出InterruptedException,从而导致无限循环。
                }
            }
        } finally {
            //在任务返回时检查本地保存的中断状态,根据情况恢复中断状态。
            if (interrupted)
                Thread.currentThread().interrupt();
        }
    }
}

计时任务示例:

在前面的“任务取消”下的示例代码中,aSecondOfPrimes()方法在task线程执行过程中,如果抛出一个非检查异常,可能会被忽略,因为task线程和调用aSecondOfPrimes()的线程不是同一个。

下面的示例解决了忽略非检查异常的问题。

/**
 * InterruptBorrowedThread
 * <p/>
 * Scheduling an interrupt on a borrowed thread
 *
 * @author Brian Goetz and Tim Peierls
 */
public class TimedRun1 {
    private static final ScheduledExecutorService cancelExec =         
                                   Executors.newScheduledThreadPool(1);

    public static void timedRun(Runnable r,
                                long timeout, TimeUnit unit) {
        final Thread taskThread = Thread.currentThread();//task线程是currentThread
        cancelExec.schedule(new Runnable() {
            public void run() {
                taskThread.interrupt();//设置中断状态
            }
        }, timeout, unit);
        r.run();//在调用者线程(timedRun线程)执行任务,会捕获非检查异常
    }
}

TimeRun1的task线程是currentThread,即与调用timedRun的线程(简称timedRun线程)是同一个,但是违反了之前说的一条准则:每个线程拥有自己的中断策略,除非知道中断对该线程的意义,否则不应该中断该线程。如果任务在超时之前完成,那么会在返回timedRun线程之后,执行取消任务,即taskThread.interrupt(),会中断timedRun线程,这种情况下并不知道会进行说明操作。

下面是另外一种:

/**
 * TimedRun2
 * <p/>
 * Interrupting a task in a dedicated thread
 *
 * @author Brian Goetz and Tim Peierls
 */
public class TimedRun2 {
    private static final ScheduledExecutorService cancelExec =             
                                         newScheduledThreadPool(1);
    public static void timedRun(final Runnable r,
                                long timeout, TimeUnit unit)
            throws InterruptedException {
        class RethrowableTask implements Runnable {
            private volatile Throwable t;
            public void run() {
                try {
                    r.run();
                } catch (Throwable t) {
                    this.t = t;
                }
            }
            void rethrow() {
                if (t != null)
                    throw launderThrowable(t);
            }
        }
        RethrowableTask task = new RethrowableTask();
        final Thread taskThread = new Thread(task);//task线程,不再是currentThread
        taskThread.start();
        cancelExec.schedule(new Runnable() {
            public void run() {
                taskThread.interrupt();
            }
        }, timeout, unit);
        taskThread.join(unit.toMillis(timeout));//task线程先执行
        task.rethrow();//重新抛出异常
    }

TimeRun2的task线程是另外一个独立的线程,与timedRun线程不是同一个。task线程启动后,调用join,限时返回。如果在超时之前任务完成并返回,那么之后执行取消任务,通过taskThread.interrupt()设置中断状态,并不会中断timedRun线程。之后通过rethrow()方法重新抛出任务执行过程中的异常(如果有的话),注意这里Throwable t变量在timedRun线程和task线程之间共享,所以声明为了volatile类型。

通过Future来实现取消

ExecutorService.submit返回一个Future。可以通过Future.cancel来取消任务。

public interface Future<V> {

    /**
     *尝试取消任务的执行。
     *如果任务已经完成或被取消或因为其他原因不能被取消,此操作失败。
     *如果成功,调用cancel时,未开始的任务将不会开始。
     *已经开始的任务,由参数mayInterruptIfRunning决定是否中断执行该任务的线程。
     *
     *这个方法返回之后,后续调用isDone都返回true。如果此方法返回true,后续调用isCancelled都返回true。
     *
     *
     *mayInterruptIfRunning为true,表示正在执行这个任务的线程应该被中断,否则允许执行完成。
     */
    boolean cancel(boolean mayInterruptIfRunning);

    /**
     * 任务正常完成之前被取消,则返回true。
     */
    boolean isCancelled();

    /**
     * Returns {@code true} if this task completed.
     *
     * 中断、异常、被取消都是completed。
     */
    boolean isDone();

    /**
     * 阻塞直到任务完成。
     *
     * 如果任务被取消,抛出CancellationException
     * 如果任务抛出异常,则抛出ExecutionException
     * 如果当前线程被中断,则抛出InterruptedException 
     */
    V get() throws InterruptedException, ExecutionException;

    V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}

 

处理不可中断的阻塞

这要求知道线程阻塞的原因,从而采用对应的方式。

处理非标准的取消操作

1.改写interrupt方法

public class ReaderThread extends Thread {
    private static final int BUFSZ = 512;
    private final Socket socket;
    private final InputStream in;

    public ReaderThread(Socket socket) throws IOException {
        this.socket = socket;
        this.in = socket.getInputStream();
    }

    public void interrupt() {
        try {
            socket.close();//关闭底层套接字。(InputStream和OutStream中的read和write方法不会响应中断,通过关闭底层套接字,可以使由于执行read或write等方法而被阻塞的线程抛出一个SocketException)
        } catch (IOException ignored) {
        } finally {
            super.interrupt();//处理正常中断(可以响应中断的阻塞)
        }
    }

    public void run() {
        try {
            byte[] buf = new byte[BUFSZ];
            while (true) {
                int count = in.read(buf);
                if (count < 0)
                    break;
                else if (count > 0)
                    processBuffer(buf, count);
            }
        } catch (IOException e) { /* Allow thread to exit */
        }
    }

    public void processBuffer(byte[] buf, int count) {
    }
}

2.采用newTaskFor封装非标准的取消

newTaskFor是ThreadPoolExecutor的一个工厂方法,接收一个Callable,返回一个FutureTask。

    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
        return new FutureTask<T>(callable);
    }

FutureTask实现来RunnableFuture,RunnableFuture继承了Future和Runnable。可以通过重写newTaskFor方法,使其能够返回自定义的FutureTask,并重写FutureTask的cancel方法,使其包含非标准的取消。

示例:

//定义一个CancellingExecutor,继承ThreadPoolExecutor,并重写其newTaskFor方法,使其能够返回自定义任务或原标准任务。
@ThreadSafe
class CancellingExecutor extends ThreadPoolExecutor {
    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
        if (callable instanceof CancellableTask)
            return ((CancellableTask<T>) callable).newTask();
        else
            return super.newTaskFor(callable);
    }
}

//定义一个接口,“可取消任务”接口
interface CancellableTask <T> extends Callable<T> {
    void cancel();//这个方法实现非标准的取消。

    RunnableFuture<T> newTask();//这个方法实现自定义的FutureTask。
}

//应用,SocketUsingTask实现了“可取消接口”,
public abstract class SocketUsingTask <T> implements CancellableTask<T> {
    @GuardedBy("this") private Socket socket;

    protected synchronized void setSocket(Socket s) {
        socket = s;
    }
    
    //这里实现了非标准的取消,即关闭底层套接字。
    public synchronized void cancel() {
        try {
            if (socket != null)
                socket.close();
        } catch (IOException ignored) {
        }
    }
    
    //这里实现了返回自定义的FutureTask,并重写了其cancel方法,使其包含了非标准的取消,以及标准的取消。
    public RunnableFuture<T> newTask() {
        return new FutureTask<T>(this) {
            public boolean cancel(boolean mayInterruptIfRunning) {
                try {
                    SocketUsingTask.this.cancel();
                } finally {
                    return super.cancel(mayInterruptIfRunning);
                }
            }
        };
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值