Java多线程---枪有20发子弹,实现子弹不停上膛射出的过程,生产者消费者

场景:一支枪可盛20发子弹,运用多线程,实现子弹不停上膛、射出的过程。
一、基于信号量实现
二、基于ReentrantLock实现

private static Semaphore notFull = new Semaphore(20);
    private static Semaphore notEmpty = new Semaphore(0);

    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(6);
        pool.execute(new Gun(notEmpty, notFull));
        pool.execute(new Bullet(notEmpty, notFull));
    }

    public static class Gun implements Runnable {

        private Semaphore notEmpty;
        private Semaphore notFull;

        public Gun(Semaphore notEmpty, Semaphore notFull) {
            this.notEmpty = notEmpty;
            this.notFull = notFull;
        }

        public void run() {

            while (true) {
                try {
                    notEmpty.acquire();
                    System.out.println("射击---biubiubiu");
                    notFull.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public static class Bullet implements Runnable {

        private Semaphore notEmpty;
        private Semaphore notFull;

        public Bullet(Semaphore notEmpty, Semaphore notFull) {
            this.notEmpty = notEmpty;
            this.notFull = notFull;
        }

        public void run() {
            while (true) {
                try {
                    notFull.acquire();
                    System.out.println("压入子弹---铛铛");
                    notEmpty.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

基于ReentrantLock实现

    private static Lock lock = new ReentrantLock();
    private static Condition notEmpty = lock.newCondition();
    private static Condition notFull = lock.newCondition();
    private static volatile  int count = 0;
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(6);
        pool.execute(new Gun(lock, notEmpty, notFull));
        pool.execute(new Bullet(lock, notEmpty, notFull));
    }

    public static class Gun implements Runnable {
        private Lock lock;
        private Condition notEmpty;
        private Condition notFull;

        public Gun(Lock lock, Condition notEmpty, Condition notFull) {
            this.lock = lock;
            this.notEmpty = notEmpty;
            this.notFull = notFull;
        }

        public void run() {
            while (true) {
                lock.lock();
                while (count == 0) {
                    try {
                        notEmpty.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("射击---biubiubiu");
                count--;
                notFull.signal();
                lock.unlock();
            }
        }
    }

    public static class Bullet implements Runnable {

        private Lock lock;
        private Condition notEmpty;
        private Condition notFull;

        public Bullet(Lock lock, Condition notEmpty, Condition notFull) {
            this.lock = lock;
            this.notEmpty = notEmpty;
            this.notFull = notFull;
        }

        public void run() {
            while (true) {
                lock.lock();
                while (count >= 20) {
                    try {
                        notFull.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("压入子弹---铛铛");
                count++;
                notEmpty.signal();
                lock.unlock();
            }
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 蝴蝶你们认为是一种美丽的昆虫 但在它美丽的外壳下它是怎样的 蝴蝶也很残忍 小美:哇!你看这有好多蝴蝶啊!好漂亮哎!...
    死寂梦阅读 133评论 0 0
  • 昨晚上刚聊完后悔,接着看私塾的课程就是灰犀牛这本书。顿然觉得负面灰犀牛这个事情,跟昨天谈到的后悔过程是那么类似。所...
    读书的二哈阅读 453评论 0 0
  • 每当我们收拾东西的时候,是不是看到很多旧物不舍得扔,又用不上。这时候就需要我们来一次断舍离。 断舍离这里是指网络语...
    沙漠明珠阅读 784评论 0 5
  • “江,节日快乐。” 深夜十二点刷着手机的某副队在看到这条无比准时的消息的时候还是愣了一下,队长竟然还没睡吗?明天上...
    苏暮黎阅读 291评论 2 1