来源 | why技术
头图 | 视觉中国
https://springboot.io/t/topic/1139
public class ParkDemo { public static void main(String[] args) throws InterruptedException {
Integer parkSpace = 3; System.out.println("这里有" + parkSpace + "个停车位,先到先得啊!"); Semaphore semaphore = new Semaphore(parkSpace, true);
Thread threadA = new Thread(new ParkCar(1, "布加迪", semaphore), "赵四"); Thread threadB = new Thread(new ParkCar(2, "法拉利", semaphore), "刘能、谢广坤"); Thread threadC = new Thread(new ParkCar(1, "劳斯莱斯", semaphore), "why哥");
threadA.start(); threadB.start(); threadC.start(); }}
class ParkCar implements Runnable { private int n; private String carName; private Semaphore semaphore;
public ParkCar(int n, String carName, Semaphore semaphore) { this.n = n; this.carName = carName; this.semaphore = semaphore; }
@Override public void run() { try { if (semaphore.availablePermits() < n) { System.out.println(Thread.currentThread().getName() + "来停车,但是停车位不够了,等着吧"); } semaphore.acquire(n); System.out.println(Thread.currentThread().getName() + "把自己的" + carName + "停进来了,剩余停车位:" + semaphore.availablePermits() + "辆"); //模拟停车时长 int parkTime = ThreadLocalRandom.current().nextInt(1, 6); TimeUnit.SECONDS.sleep(parkTime); System.out.println(Thread.currentThread().getName() + "把自己的" + carName + "开走了,停了" + parkTime + "小时"); } catch (Exception e) { e.printStackTrace(); } finally { semaphore.release(n); System.out.println(Thread.currentThread().getName() + "走后,剩余停车位:" + semaphore.availablePermits() + "辆"); } }}
public class ParkDemo { public static void main(String[] args) throws InterruptedException {
Integer parkSpace = 3; System.out.println("这里有" + parkSpace + "个停车位,先到先得啊!"); Semaphore semaphore = new Semaphore(parkSpace, true);
Thread threadA = new Thread(new ParkCar(1, "布加迪", semaphore), "赵四"); Thread threadB = new Thread(new ParkCar(2, "法拉利", semaphore), "刘能、谢广坤"); Thread threadC = new Thread(new ParkCar(1, "劳斯莱斯", semaphore), "why哥");
threadA.start(); threadC.start(); threadB.start(); //模拟大爷劝退 threadB.interrupt(); }}
class ParkCar implements Runnable {
private int n; private String carName; private Semaphore semaphore;
public ParkCar(int n, String carName, Semaphore semaphore) { this.n = n; this.carName = carName; this.semaphore = semaphore; }
@Override public void run() { try { if (semaphore.availablePermits() < n) { System.out.println(Thread.currentThread().getName() + "来停车,但是停车位不够了,等着吧"); } semaphore.acquire(n); System.out.println(Thread.currentThread().getName() + "把自己的" + carName + "停进来了," + "剩余停车位:" + semaphore.availablePermits() + "辆"); //模拟停车时长 int parkTime = ThreadLocalRandom.current().nextInt(1, 6); TimeUnit.SECONDS.sleep(parkTime); System.out.println(Thread.currentThread().getName() + "把自己的" + carName + "开走了,停了" + parkTime + "小时"); } catch (InterruptedException e) { System.err.println(Thread.currentThread().getName() + "被门口大爷劝走了。"); } finally { semaphore.release(n); System.out.println(Thread.currentThread().getName() + "走后,剩余停车位:" + semaphore.availablePermits() + "辆"); } }}
更多精彩推荐 ☞ 在这场“豪华围猎”中,字节跳动的海外野心还要被困宥多久? ☞开源项目如何挣钱?Spark 商业化公司创始人曝光心路历程 ☞ 科技股疯狂造富的背后,“泡沫”离我们到底有多远? ☞ PyTorch 1.6、TensorFlow 2.3、Pandas 1.1同日发布!都有哪些新特性? ☞ 程序员必备基础:Git 命令全方位学习 ☞ 公链还能这样玩?二次元、出圈与社区自治 ![]()
点分享 ![]()
点点赞 ![]()
点在看