Semaphore信号量模拟停车厂停车

Semaphore是信号量,acquire()表示获取,release()释放。
只有当有空闲的信号量的时候,其他线程才能进入。

SemaphoreDemo代码如下:

/**
 * 模拟停车厂停车,用完释放,其他车才能使用
 */
public class SemaphoreDemo {

    private static Semaphore s = new Semaphore(2); // 2个停车位

    static class ParkTask implements Runnable {

        @Override
        public void run() {

            try {
                s.acquire(); // 占用停车位
                long l =(2+(int)(Math.random()*(5-2)))*1000;
                System.out.println(Thread.currentThread().getName() + "停车用时"+ l );
                TimeUnit.MILLISECONDS.sleep(l); // 比Thread.sleep()更人性化吧
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                s.release(); // 释放停车位
            }
        }

        public static void main(String[] args) {
            System.out.println("最多只有2个车位,停车开始...");
            ExecutorService pool = Executors.newCachedThreadPool();
            pool.submit(new ParkTask());
            pool.submit(new ParkTask());
            pool.submit(new ParkTask());
            pool.submit(new ParkTask());
            pool.submit(new ParkTask());
            pool.submit(new ParkTask());
            pool.shutdown();
        }
    }
}
发布了422 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/enthan809882/article/details/104178059