【Java】多线程:停车场模拟
需求
模拟停车场停车
效果如图
实现步骤
- 构造信号量Semaphore
- 从信号量中获取许可证:acquire()从信号量
- sleep()模拟停车
- release():模拟离开停车场,归还许可证,并唤醒等待队列中的车辆线程
代码
package com.cxl.demo01_erupt;
import java.util.concurrent.Semaphore;
//停车场问题
public class Demo01_01_parklot {
static Semaphore semaphore = new Semaphore(1); //1个车位
public static voi