美文网首页
Java并发 | ReentrantLock实现生产者消费者

Java并发 | ReentrantLock实现生产者消费者

作者: icebreakeros | 来源:发表于2019-08-08 20:18 被阅读0次

    ReentrantLock实现生产者消费者

    一对一交替打印

    class Service {
    
        private Lock lock = new ReentrantLock();
        private Condition condition = lock.newCondition();
        private boolean hasValue = false;
    
        public void set() {
            try {
                lock.lock();
                while (hasValue) {
                    condition.await();
                }
                System.out.println("★");
                hasValue = true;
                condition.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    
        public void get() {
            try {
                lock.lock();
                while (!hasValue) {
                    condition.await();
                }
                System.out.println("☆");
                hasValue = false;
                condition.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
    
    class ProducerOfR implements Runnable {
    
        private Service service;
    
        public ProducerOfR(Service service) {
            this.service = service;
        }
    
        @Override
        public void run() {
            while (true) {
                service.set();
            }
        }
    }
    
    class ConsumerOfR implements Runnable {
    
        private Service service;
    
        public ConsumerOfR(Service service) {
            this.service = service;
        }
    
        @Override
        public void run() {
            while (true) {
                service.get();
            }
        }
    }
    
    public class Run {
    
        public static void main(String[] args) throws InterruptedException {
            Service service = new Service();
            Thread pThread = new Thread(new ProducerOfR(service));
            Thread cThread = new Thread(new ConsumerOfR(service));
            pThread.start();
            cThread.start();
        }
    }
    

    多对多交替打印

    class Service {
    
        private Lock lock = new ReentrantLock();
        private Condition condition = lock.newCondition();
        private boolean hasValue = false;
    
        public void set() {
            try {
                lock.lock();
                while (hasValue) {
                    System.out.println("★★");
                    condition.await();
                }
                System.out.println("★");
                hasValue = true;
                condition.signalAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    
        public void get() {
            try {
                lock.lock();
                while (!hasValue) {
                    System.out.println("☆☆");
                    condition.await();
                }
                System.out.println("☆");
                hasValue = false;
                condition.signalAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
    
    class ProducerOfR implements Runnable {
    
        private Service service;
    
        public ProducerOfR(Service service) {
            this.service = service;
        }
    
        @Override
        public void run() {
            while (true) {
                service.set();
            }
        }
    }
    
    class ConsumerOfR implements Runnable {
    
        private Service service;
    
        public ConsumerOfR(Service service) {
            this.service = service;
        }
    
        @Override
        public void run() {
            while (true) {
                service.get();
            }
        }
    }
    
    public class Run {
    
        public static void main(String[] args) throws InterruptedException {
            Service service = new Service();
            Thread[] p = new Thread[10];
            Thread[] c = new Thread[10];
            for (int i = 0; i < 10; i++) {
                p[i] = new Thread(new ProducerOfR(service));
                c[i] = new Thread(new ConsumerOfR(service));
                p[i].start();
                c[i].start();
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Java并发 | ReentrantLock实现生产者消费者

          本文链接:https://www.haomeiwen.com/subject/bqpfjctx.html