Latch模式--多线程同步

1,相关文章推荐

Java多线程---CountDownLatch用法实例

POI结合线程池批量处理导入减少导入时间

2,直接源码

package com.wayne.latchPattern;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

public abstract class Latch {

    protected int limit;

    public Latch(int limit){
        this.limit = limit;
    }

    public abstract void await() throws InterruptedException;

    public abstract void await(TimeUnit timeUnit,long time) throws InterruptedException, WaitTimeoutException;

    public abstract void countdown();

    public abstract int getUnArrived();
}
package com.wayne.latchPattern;

import java.util.concurrent.TimeUnit;

public class CountDownLatch extends Latch {

    public CountDownLatch(int limit){
        super(limit);
    }

    @Override
    public void await() throws InterruptedException {
        synchronized (this){
            while (limit >0) {
                this.wait();
            }
        }
    }

    @Override
    public void await(TimeUnit timeUnit, long time) throws InterruptedException,WaitTimeoutException {
        if(time <= 0 ){
            throw new IllegalArgumentException("time is invaild.");
        }

        long remainingNanos = timeUnit.toNanos(time);
        final long endNanos = System.nanoTime() + remainingNanos;
        synchronized (this){
            while (limit > 0){
                if(TimeUnit.NANOSECONDS.toMillis(remainingNanos)<= 0){
                    throw new WaitTimeoutException("The wait time over specify time.");
                }

                this.wait(TimeUnit.NANOSECONDS.toMillis(remainingNanos));
                remainingNanos = endNanos - System.nanoTime();
            }
        }
    }

    @Override
    public void countdown() {
        synchronized (this){
            if(limit <= 0){
                throw new IllegalStateException(" all arrived");
            }

            limit--;
            this.notifyAll();
        }
    }

    @Override
    public int getUnArrived() {
        synchronized (this){
            return limit;
        }
    }
}
package com.wayne.latchPattern;

import org.omg.CORBA.TIMEOUT;

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class ProgrammerTravel extends Thread {

    private final Latch latch;

    private final String programmer;

    private final String transport;

    public ProgrammerTravel(Latch latch,String programmer,String transport){
        this.latch = latch;
        this.programmer = programmer;
        this.transport = transport;
    }

    @Override
    public void run() {
        try{
            System.out.println(programmer+" start to move with "+ transport);
            TimeUnit.SECONDS.sleep(new Random(System.currentTimeMillis()).nextInt(10));
        } catch (Exception e){
            e.printStackTrace();
        }

        System.out.println(programmer+" with "+transport+ " has arrived!");
        latch.countdown();
    }
}
package com.wayne.latchPattern;

public class WaitTimeoutException extends Exception {

    public WaitTimeoutException(String message){
        super(message);
    }
}
package com.wayne.latchPattern;

import java.util.concurrent.TimeUnit;

public class LatchPatternTest {

    public static void main(String[] args) throws InterruptedException, WaitTimeoutException {
        Latch latch = new CountDownLatch(4);
        new ProgrammerTravel(latch,"Wayne","Bicycle").start();
        new ProgrammerTravel(latch,"Bruce","Bus").start();
        new ProgrammerTravel(latch,"Batman","Car").start();
        new ProgrammerTravel(latch,"IronMan","Flight").start();
        latch.await(TimeUnit.SECONDS,5);

        System.out.println("All arrived,let's start!");
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值