zoukankan      html  css  js  c++  java
  • 多生产者多消费者,生产一个数据,消费一个数据

    方式一

    /**
     * 多个线程要操作的数据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()
            );
        }
    }
  • 相关阅读:
    [cf582E]Boolean Function
    [atAGC029F]Construction of a tree
    [atAGC020E]Encoding Subsets
    [gym102769L]Lost Temple
    [atAGC034E]Complete Compress
    [cf566E]Restoring Map
    [atAGC023F]01 on Tree
    [gym102822I]Invaluable Assets
    [gym102900H]Rice Arrangement
    [Offer收割]编程练习赛32
  • 原文地址:https://www.cnblogs.com/moris5013/p/11710430.html
Copyright © 2011-2022 走看看