多线程学习:多线程习题(三)

代码都已上传github

P03 有三个车库,模拟多个用户停车、离开的效果

心得

  • 利用上消费者/生产者原理
  • 用上BlockingQueue 阻塞队列来进行停车和离开消息的通讯。
  • 在单元测试的时候,如果单元测试的线程结束了,子线程也会跟着结束。

代码块

public class P03 {
    /*有三个车库,模拟多个用户停车、离开的效果*/
    private ReentrantLock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    private BlockingQueue blockingQueue =new ArrayBlockingQueue<>(3);

    public void parking() throws InterruptedException {


        try {
            lock.lock();
            while (blockingQueue.size()==3) {
                condition.await();
            }
            blockingQueue.add(Thread.currentThread());
            System.out.println("第" + Thread.currentThread().getName() + "号车停车了");
            System.out.println("已停有"+blockingQueue.size()+"辆车");
            condition.signal();

        } finally {
            lock.unlock();
        }

    }

    public void outing() throws InterruptedException {
        try {
            lock.lock();
            while (blockingQueue.isEmpty()) {
                condition.await();
            }
            Thread peek = blockingQueue.remove();
            System.out.println("第" + peek.getName() + "号车离开了");
            System.out.println("已停有"+blockingQueue.size()+"辆车");
            condition.signal();
        } finally {
            lock.unlock();
        }
    }

}

class park implements Runnable{

    private P03 p03;

    public park(P03 p03) {
        super();
        this.p03 = p03;
    }

    @Override
    public void run() {
        try {
            p03.parking();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

class out implements Runnable{
    private P03 p03;

    public out(P03 p03) {
        super();
        this.p03 = p03;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(100);
            p03.outing();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

效果图

image.png

你可能感兴趣的:(多线程学习:多线程习题(三))