• 生产者和消费者简单实现


    一、synchronized实现

    package com.duchong.queue;
    
    /**
     * @author DUCHONG
     * @since 2020-09-17 18:23
     **/
    public class SynchronizedDemo {
    
        //对象锁
        static Object lock=new Object();
        //剩余资源数量
        static int leftCount=0;
        //最大资源数量
        static int maxLimit=5;
    
        public static void main(String[] args)throws Exception {
    
            SynchronizedDemo synchronizedDemo = new SynchronizedDemo();
    
            new Thread(synchronizedDemo.new SyncProvider(),"provider-thread").start();
            new Thread(synchronizedDemo.new SyncConsumer(),"consumer-thread").start();
    
        }
    
        /**
         * 生产者
         */
        class SyncProvider implements Runnable{
    
            @Override
            public void run() {
    
                System.out.println(Thread.currentThread().getName()+"---启动");
    
                synchronized (lock) {
    
                    while (leftCount <= maxLimit) {
    
                        leftCount++;
                        System.out.println(Thread.currentThread().getName()+"---生产者生产资源---" + leftCount);
    
                        try {
    
                            if(leftCount==maxLimit) {
                                System.out.println(Thread.currentThread().getName()+"---生产者开始阻塞---释放锁---" + leftCount);
                                lock.wait();
                            }
                            //通知消费者
                            lock.notifyAll();
                        }
                        catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    
        /**
         * 消费者
         */
        class SyncConsumer implements Runnable{
    
    
            @Override
            public void run() {
    
                System.out.println(Thread.currentThread().getName()+"---启动");
                synchronized (lock){
                    //数量为0时,等待
                    while (leftCount>0){
    
                        System.out.println(Thread.currentThread().getName()+"---消费者消费资源---"+leftCount);
                        leftCount--;
                        try {
    
                            if(leftCount==0) {
                                System.out.println(Thread.currentThread().getName()+"---消费者开始阻塞---" + leftCount);
                                lock.wait();
                            }
                            //通知生产者
                            lock.notifyAll();
                        }
                        catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    
    
    

    二、ReentrantLock实现

    package com.duchong.queue;
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * @author DUCHONG
     * @since 2020-09-18 14:52
     **/
    public class ReentrantLockDemo {
    
        //剩余资源数量
        static int leftCount=0;
        //最大资源数量
        static int maxLimit=5;
        //一个lock对象
        Lock lock=new ReentrantLock();
        Condition provider=lock.newCondition();
        Condition consumer=lock.newCondition();
    
        public static void main(String[] args) {
            ReentrantLockDemo lockDemo = new ReentrantLockDemo();
    
            new Thread(lockDemo.new LockProvider(),"provider-thread").start();
            new Thread(lockDemo.new LockConsumer(),"consumer-thread").start();
        }
    
        /**
         * 生产者
         */
        class LockProvider implements Runnable{
    
            @Override
            public void run() {
    
                System.out.println(Thread.currentThread().getName()+"---启动");
    
                lock.lock();
    
                try {
                    while (leftCount <= maxLimit) {
    
                        leftCount++;
                        System.out.println(Thread.currentThread().getName()+"---生产者生产资源---" + leftCount);
    
                        if(leftCount==maxLimit) {
                            System.out.println(Thread.currentThread().getName()+"---生产者开始阻塞---释放锁---" + leftCount);
                            provider.await();
                        }
                        //通知消费者
                        consumer.signalAll();
                    }
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally {
                    //释放锁
                    lock.unlock();
                }
            }
        }
    
        /**
         * 消费者
         */
        class LockConsumer implements Runnable{
    
    
            @Override
            public void run() {
    
                System.out.println(Thread.currentThread().getName()+"---启动");
                lock.lock();
                try {
                    //数量为0时,等待
                    while (leftCount>0){
    
                        System.out.println(Thread.currentThread().getName()+"---消费者消费资源---"+leftCount);
                        leftCount--;
                        if(leftCount==0) {
                            System.out.println(Thread.currentThread().getName()+"---消费者开始阻塞---" + leftCount);
                            consumer.await();
                        }
                        //通知生产者
                        provider.signalAll();
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    lock.unlock();
                }
            }
        }
    }
    

    三、BlockQueue实现

    package com.duchong.queue;
    
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author DUCHONG
     * @since 2020-09-17 18:23:09
     **/
    public class LinkBlockQueueDemo {
    
    
        private static final int MAX_CAPACITY = 20;
        private static LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>(MAX_CAPACITY);
    
        public static void main(String[] args)throws Exception {
    
            new Thread(new Provider(queue),"provider-thread").start();
    
            TimeUnit.SECONDS.sleep(2L);
    
            new Thread(new Consumer(queue),"consumer-thread").start();
    
        }
    }
    
    class Consumer implements Runnable {
    
        LinkedBlockingQueue queue;
        public Consumer(LinkedBlockingQueue queue) {
            this.queue=queue;
        }
    
        @Override
        public void run() {
    
            System.out.println("---"+Thread.currentThread().getName()+"---启动");
            while (true) {
                try {
    
                    System.out.println(Thread.currentThread().getName()+"---消费---" + queue.take());
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
     class Provider implements Runnable {
    
         LinkedBlockingQueue queue;
         public Provider(LinkedBlockingQueue queue) {
             this.queue=queue;
         }
    
         @Override
        public void run() {
    
             System.out.println("---"+Thread.currentThread().getName()+"---启动");
    
            while (true) {
                try {
                    for (int i = 1; i <= 30; i++) {
                        queue.put("food-"+i);
                        System.out.println(Thread.currentThread().getName()+"---生产---food-"+i);
                    }
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
  • 相关阅读:
    hibernate入门
    struts文件上传
    Struts的增删改查
    struts入门
    Maven配置以及环境搭配
    layui增删改查
    easyui三
    A
    C. Permutation Cycle
    E
  • 原文地址:https://www.cnblogs.com/geekdc/p/13784443.html
Copyright © 2020-2023  润新知