• 生产者消费者模型的四种java编程例子(转载)


    package producer_customer;  
      
    public class ProducerCustomer1 {  
        public static int limit = 10;  
        public static int have = 0;  
        public static int start = 0;  
        Task[] tasklist = new Task[limit];  
      
        class Producer implements Runnable {  
            @Override  
            public void run() {  
                while (true) {  
                    synchronized (tasklist) {  
                        if (ProducerCustomer1.have < ProducerCustomer1.limit) {  
                            Task temp = new Task(  
                                    "mytest"  
                                            + (ProducerCustomer1.start + ProducerCustomer1.have)  
                                            % ProducerCustomer1.limit+" "+Thread.currentThread().getId());  
                              
                            tasklist[(ProducerCustomer1.start + ProducerCustomer1.have)  
                                    % ProducerCustomer1.limit] = temp;  
                            ProducerCustomer1.have++;  
                            tasklist.notifyAll();  
                        } else {  
                            try {  
                                tasklist.wait();  
                            } catch (InterruptedException e) {  
                                e.printStackTrace();  
                            }  
                        }  
                    }  
                }  
      
            }  
        }  
      
        class Customer implements Runnable {  
            @Override  
            public void run() {  
                while (true) {  
                    Task temp = null;  
                    synchronized (tasklist) {  
                        if (ProducerCustomer1.have > 0) {  
                            temp = tasklist[ProducerCustomer1.start];  
                            ProducerCustomer1.start = (ProducerCustomer1.start + 1)  
                                    % ProducerCustomer1.limit;  
                            ProducerCustomer1.have--;  
                            System.out.println(temp.taskname + ":"  
                                    + Thread.currentThread().getId());  
                            tasklist.notifyAll();  
                        } else {  
                            try {  
                                tasklist.wait();  
                            } catch (InterruptedException e) {  
                                e.printStackTrace();  
                            }  
                        }  
      
                    }  
                }  
            }  
        }  
      
        class Task {  
            public String taskname;  
            public Task(String taskname) {  
                this.taskname = taskname;  
            }  
        }  
        public static void main(String[] args) {  
            ProducerCustomer1 p = new ProducerCustomer1();  
            Producer producer = p.new Producer();  
            Customer customer = p.new Customer();  
            new Thread(producer).start();  
            new Thread(producer).start();  
            new Thread(producer).start();  
            new Thread(producer).start();  
            new Thread(customer).start();  
            new Thread(customer).start();  
            new Thread(customer).start();  
        }  
    }  


    [java] view plaincopy
    package producer_customer;  
      
    import java.util.concurrent.locks.Condition;  
    import java.util.concurrent.locks.Lock;  
    import java.util.concurrent.locks.ReentrantLock;  
      
      
    public class ProducerCustomer2 {  
      
        public static int limit = 10;  
        public static int have = 0;  
        public static int start = 0;  
        Task[] tasklist = new Task[limit];  
        private final Lock lock = new ReentrantLock();  
        private final Condition full = lock.newCondition();  
        private final Condition empty = lock.newCondition();  
      
        class Producer implements Runnable {  
            @Override  
            public void run() {  
                while (true) {  
                    lock.lock();  
                    if (ProducerCustomer2.have < ProducerCustomer2.limit) {  
                        Task temp = new Task(  
                                "mytest"  
                                        + (ProducerCustomer2.start + ProducerCustomer2.have)  
                                        % ProducerCustomer2.limit + " "  
                                        + Thread.currentThread().getId());  
                        tasklist[(ProducerCustomer2.start + ProducerCustomer2.have)  
                                % ProducerCustomer2.limit] = temp;  
                        ProducerCustomer2.have++;  
                        empty.signalAll();  
                    } else {  
                        try {  
                            full.await();  
                        } catch (InterruptedException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                    lock.unlock();  
                }  
      
            }  
      
        }  
      
        class Customer implements Runnable {  
            @Override  
            public void run() {  
                while (true) {  
                    Task temp = null;  
                    lock.lock();  
                    if (ProducerCustomer2.have > 0) {  
                        temp = tasklist[ProducerCustomer2.start];  
                        ProducerCustomer2.start = (ProducerCustomer2.start + 1)  
                                % ProducerCustomer2.limit;  
                        ProducerCustomer2.have--;  
                        System.out.println(temp.taskname + ":"  
                                + Thread.currentThread().getId());  
                        full.signalAll();  
                    } else {  
                        try {  
                            empty.await();  
                        } catch (InterruptedException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                    lock.unlock();  
                }  
            }  
        }  
      
        class Task {  
            public String taskname;  
      
            public Task(String taskname) {  
                this.taskname = taskname;  
            }  
        }  
      
        public static void main(String[] args) {  
            ProducerCustomer2 p = new ProducerCustomer2();  
            Producer producer = p.new Producer();  
            Customer customer = p.new Customer();  
            new Thread(producer).start();  
            new Thread(producer).start();  
            new Thread(producer).start();  
            new Thread(producer).start();  
            new Thread(customer).start();  
            new Thread(customer).start();  
            new Thread(customer).start();  
        }  
    }  


    [java] view plaincopy
    package producer_customer;  
      
    import java.util.concurrent.LinkedBlockingQueue;  
      
    public class ProducerCustomer3 {  
        public static int limit = 10;  
        public static int have = 0;  
        public static int start = 0;  
        LinkedBlockingQueue queue = new LinkedBlockingQueue(limit);  
        class Customer implements Runnable {  
            @Override  
            public void run() {  
                while (true) {  
                    try {  
                        Tasks ss=queue.take();  
                        System.out.println(ss.taskname+":"+Thread.currentThread().getId());  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                }  
            }  
        }  
      
        class Producer implements Runnable {  
            @Override  
            public void run() {  
                while (true) {  
                    Tasks o = new Tasks("test"+Thread.currentThread().getId());  
                     try {  
                        queue.put(o);  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                      
                }  
            }  
        }  
      
        class Tasks {  
            public String taskname;  
      
            public Tasks(String taskname) {  
                this.taskname = taskname;  
            }  
        }  
        public static void main(String[] args) {  
            ProducerCustomer3 p = new ProducerCustomer3();  
            Producer producer = p.new Producer();  
            Customer customer = p.new Customer();  
            new Thread(producer).start();  
            new Thread(producer).start();  
            new Thread(producer).start();  
            new Thread(producer).start();  
            new Thread(customer).start();  
            new Thread(customer).start();  
            new Thread(customer).start();  
        }  
    }  



    [java] view plaincopy
    package producer_customer;  
      
    import java.util.concurrent.Semaphore;  
      
    public class ProducerCustomer4 {  
        Semaphore mutex = new Semaphore(1);// 互斥量  
        Semaphore notfull = new Semaphore(10);  
        Semaphore notempty = new Semaphore(0);  
        String[] array = new String[10];  
        int putptr, takeptr, count = 0;  
      
        class Producer implements Runnable {  
            @Override  
            public void run() {  
                while (true) {  
                    try {  
                        notfull.acquire();  
                        mutex.acquire();  
                        if (putptr == array.length)  
                            putptr = 0;  
                        String temp = new String("temp" + ":" + putptr + ":"  
                                + Thread.currentThread().getId());  
                        array[putptr] = temp;  
                        putptr++;  
                        count++;  
                        mutex.release();  
                        notempty.release();  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                }  
            }  
        }  
      
        class Consumer implements Runnable {  
            @Override  
            public void run() {  
                while (true) {  
                    try {  
                        notempty.acquire();  
                        mutex.acquire();  
                        if (takeptr == array.length)  
                            takeptr = 0;  
                        System.out.println(array[takeptr] + ":" + takeptr + ":"  
                                + Thread.currentThread().getId());  
                        takeptr++;  
                        count--;  
                        mutex.release();  
                        notfull.release();  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                }  
            }  
        }  
      
        public static void main(String[] args) {  
            ProducerCustomer4 s = new ProducerCustomer4();  
            Producer producer = s.new Producer();  
            Consumer consumer = s.new Consumer();  
            new Thread(producer).start();  
            new Thread(producer).start();  
            new Thread(producer).start();  
            new Thread(consumer).start();  
            new Thread(consumer).start();  
        }  
    }  

  • 相关阅读:
    cmd 新建空文件
    查看Linux版本
    centos7时间调整
    正确卸载vs2015及以前版本方式
    vs2017,vs2019 无法连接到Web服务器“IIS Express”
    .netcore开发环境和服务器注意事项
    .netcore 网站启动后 502.5
    CentOS7开机报错piix4_smbus ****host smbus controller not enabled
    centos7 升级系统后,启动界面出现多个选项
    .gitkeep文件
  • 原文地址:https://www.cnblogs.com/younghome/p/4608980.html
Copyright © 2020-2023  润新知