zoukankan      html  css  js  c++  java
  • spring的全流程

    手写生产者和消费者

    使用object的wait和notify方法

    生产者

    import java.util.List;
    
    public class Producer implements Runnable {
    
        private List<String> queue;
    
        public void run() {
            synchronized (queue) {
                while (true) {
                    while (queue.size() == MqTest.MAX_COUNT) {
                        try {
                            queue.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName() + "在生产!!!!");
                    queue.add(String.valueOf(Math.random()));
                    queue.notifyAll();
                }
            }
        }
    
        public List<String> getQueue() {
            return queue;
        }
    
        public void setQueue(List<String> queue) {
            this.queue = queue;
        }
    }
    

      

    消费者

    import java.util.List;
    
    public class Comsumer implements Runnable {
        private List<String> queue;
    
        public void setQueue(List<String> queue) {
            this.queue = queue;
        }
    
        public List<String> getQueue() {
            return queue;
        }
    
        public void run() {
            synchronized (queue) {
                while (true) {
                    while (queue.size() == 0) {
                        try {
                            queue.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName() + "在消费!!!!");
                    queue.remove(0);
                    queue.notifyAll();
                }
    
            }
        }
    }

    生产过程

    import java.util.ArrayList;
    import java.util.List;
    
    public class MqTest {
    
        public static final int MAX_COUNT = 10;
    
        public static void main(String[] args) {
            List<String> queue = new ArrayList<String>();
            Producer producer = new Producer();
            producer.setQueue(queue);
            Thread t1 = new Thread(producer);
            t1.setName("生产者1");
    
            Producer producer1 = new Producer();
            producer1.setQueue(queue);
            Thread t3 = new Thread(producer1);
            t3.setName("生产者2");
    
            Producer producer2 = new Producer();
            producer2.setQueue(queue);
            Thread t4 = new Thread(producer2);
            t4.setName("生产者3");
    
    
    
            Comsumer comsumer = new Comsumer();
            comsumer.setQueue(queue);
    
    
            Thread t2 = new Thread(comsumer);
            t2.setName("消费者1");
    
            Comsumer comsumer1 = new Comsumer();
            comsumer1.setQueue(queue);
    
    
            Thread t5 = new Thread(comsumer1);
            t5.setName("消费者2");
    
            t1.start();
            t2.start();
            t3.start();
            t4.start();
            t5.start();
        }
    }
  • 相关阅读:
    并查集 示例 : poj 1703 [Find them, Catch them 帮派之争]
    《Python 第五章》条件,循环和其他语句
    《Python 第一章》基础知识
    heapSort 堆排序 / 二叉堆
    并查集 hdu 1856 示例
    KMP / hdu 1711 [找到匹配的位置并返回]
    《Python 第四章》字典
    EnumChildWindows的使用
    C#查找指定窗口的子窗口的句柄
    C#里字符串取左边N个字符,右边N个字符,从中间取N个字符的函数
  • 原文地址:https://www.cnblogs.com/zhangchiblog/p/11997224.html
Copyright © 2011-2022 走看看