Latch(阀门)设计模式

CyclicBarrier与CountDownLatch区别:

     CycliBarrier可以重用,而CountDownLatch不可以。在设计上之所以可以重用,是因为CycliBarrier中是一群线程在等一个状态,这个状态有自身打破,而CountDownLatch是一群线程在等待一个状态。这个状态由另外的线程打破。

 

CountDownLatch类实现

public class CountDownLatch extends Latch{

    public CountDownLatch(int limit){

        super(limit):

    }

    

    @Override

    pubic void await()throws InterruptedException{

        synchronized(this){

            while(limit > 0){

                this.wait();

            }

        }

    }



    @Override

    public void countDown(){

        synchronized(this){

            if(limit <= 0)

                throw new IllegalStateException("all of task already arrived");

            limit --;

            this.notifyAl();

        }

    }



    @Override

    public int getUnarrived(){

        return limit;

    }

}



带超时的CountDownLatch

public void await(long time) throws InterruptedException,WaitTimeOutException{

    if(tim <= 0)

        throw lIIegalArgumentException();

//转换成纳秒

    long remainingNanos = unit.toNanos(time); 

   final long endNanos = System.nanoTime() + remainigNanos; 

    synchronized(this){

        while(limit > 0){

                if(TimeUnit.NANOSECONDS.toMaills(remainingNanos) < =0)

                    throw new WaitTimeoutException();

                //最长堵塞存留时间

                this.wait(TimeUnit.NANOSECONDS.toMills(remainingNanos));

                remainingNanos = endNanos - System.nanoTime();

        }

    }



}

 

CycliBarrier模式退出条件

判断线程局部Generation是否与全局的Generation相等

/**

     * Main barrier code, covering the various policies.

     */

    private int dowait(boolean timed, long nanos)

        throws InterruptedException, BrokenBarrierException,

               TimeoutException {

        final ReentrantLock lock = this.lock;

        lock.lock();

        try {

            final Generation g = generation;



            if (g.broken)

                throw new BrokenBarrierException();



            if (Thread.interrupted()) {

                breakBarrier();

                throw new InterruptedException();

            }



            int index = --count;

            if (index == 0) {  // tripped

                boolean ranAction = false;

                try {

                    final Runnable command = barrierCommand;

                    if (command != null)

                        command.run();

                    ranAction = true;

                    nextGeneration();

                    return 0;

                } finally {

                    if (!ranAction)

                        breakBarrier();

                }

            }



            // loop until tripped, broken, interrupted, or timed out

            for (;;) {

                try {

                    if (!timed)

                        trip.await();

                    else if (nanos > 0L)

                        nanos = trip.awaitNanos(nanos);

                } catch (InterruptedException ie) {

                    if (g == generation && ! g.broken) {

                        breakBarrier();

                        throw ie;

                    } else {

                        // We're about to finish waiting even if we had not

                        // been interrupted, so this interrupt is deemed to

                        // "belong" to subsequent execution.

                        Thread.currentThread().interrupt();

                    }

                }



                if (g.broken)

                    throw new BrokenBarrierException();



                if (g != generation)

                    return index;



                if (timed && nanos <= 0L) {

                    breakBarrier();

                    throw new TimeoutException();

                }

            }

        } finally {

            lock.unlock();

        }

    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值