• 多生产者多消费者,生产一个数据,消费一个数据


    方式一

    /**
     * 多个线程要操作的数据index , 要放再同步块里,这里是放在lock的同步块代码里了,但是对数据的增加和减少放在了Producer类和Consumer类了,没有体现内聚性
     */
    public class Test {
    
        private Object lock = new Object();
        private volatile boolean isProduced = false;
        private int index = 0;
    
        public static void main(String[] args) {
            Test test = new Test();
            new Thread(test.new Producer(), "P1").start();
            new Thread(test.new Producer(), "P2").start();
            new Thread(test.new Consumer(), "C1").start();
            new Thread(test.new Consumer(), "C2").start();
            new Thread(test.new Consumer(), "C3").start();
        }
    
        
        class Producer implements Runnable {
    
            @Override
            public void run() {
                while (true) {
                    synchronized (lock) {
                        while (isProduced) {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                
                                e.printStackTrace();
                            }
                        }
                        index++;
                        System.out.println(Thread.currentThread().getName() + " 生产数据  " + index);
                        isProduced = true;
                        lock.notifyAll();
                    }
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
    
        }
    
        class Consumer implements Runnable {
    
            @Override
            public void run() {
                while (true) {
                    synchronized (lock) {
                        while (!isProduced) {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        System.out.println(Thread.currentThread().getName() + " 消费数据  " + index);
                        isProduced = false;
                        lock.notifyAll();
                    }
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    
            }
    
        }
    }

    方式二

    /**
     * 
     * 多个线程要操作的数据i要放在同步块里,这里是lock锁的同步块里了,对数据i的操作与数据i放在同一个类里了,体现了内聚性
     *
     */
    public class Test2{
    
        private int i = 0;
    
        final private Object LOCK = new Object();
    
        private volatile boolean isProduced = false;
    
        public void produce() {
            synchronized (LOCK) {
                while (isProduced) {
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
                i++;
                System.out.println(Thread.currentThread().getName() + " 生产 了  " + i);
                LOCK.notifyAll();
                isProduced = true;
            }
        }
    
        public void consume() {
            synchronized (LOCK) {
                while (!isProduced) {
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() +" 消费了 " + i);
                LOCK.notifyAll();
                isProduced = false;
            }
        }
    
        public static void main(String[] args) {
            Test2 pc = new Test2();
            Stream.of("P1", "P2", "P3").forEach(n ->
                    new Thread(n) {
                        @Override
                        public void run() {
                            while (true) {
                                pc.produce();
                                try {
                                    Thread.sleep(10);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }.start()
            );
            Stream.of("C1", "C2", "C3", "C4").forEach(n ->
                    new Thread(n) {
                        @Override
                        public void run() {
                            while (true) {
                                pc.consume();
                                try {
                                    Thread.sleep(10);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }.start()
            );
        }
    }
  • 相关阅读:
    SY/T 4206-2019 石油天然气建设工程施工质量验收规范 电气工程
    jmeter-05
    python-02(数组,列表,元祖,词典)的简单操作实例
    linux-13(查看文件命令find、远程传输文件scp,创建文件并更改权限)
    jmeter-04
    关于python的面试题目
    linux-12(find命令的强大搜索功能,删除命令)
    python-01(如何安装python并熟悉类型)
    小程序、app、web测试的区别
    软件测试面试问题及答案
  • 原文地址:https://www.cnblogs.com/moris5013/p/11710430.html
Copyright © 2020-2023  润新知