• java多线程:生产者和消费者模式(wait-notify) : 单生产和单消费


    单生产者

    package com.example.t.pc;
    
    import java.util.List;
    
    //生产者
    public class P {
        private List list;
    
        public P(){
        }
    
        public P(List list){
            this.list = list;
        }
    
        public void add(){
            while(true){
                synchronized (list){
                    try {
                        System.out.println("3s----------------");
                        Thread.sleep(3000);
                        if(list != null && list.size() > 0){
                            System.out.println("生产者:停止生产");
                            list.wait(); //锁释放 原地等待
                            System.out.println("P ok解锁");
                        }
    
                        list.add("123");
                        list.notify();
                        System.out.println("生产者:开始生产");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    }

    单消费者

    package com.example.t.pc;
    
    import java.util.List;
    
    //消费者
    public class C {
        private List list;
    
        public C(){
        }
    
        public C(List list){
            this.list = list;
        }
    
        public void sub(){
            while (true){
                synchronized (list){
                    try {
                        System.out.println("1s----------------");
                        Thread.sleep(1000);
                        if(list != null && list.size() > 0){
                            list.remove(0);
                            list.notify();
                            System.out.println("消费者: 开始消费");
                        }else{
                            System.out.println("消费者: 停止消费");
                            list.wait();//锁释放 原地等待
                            System.out.println("C ok解锁");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    执行

    package com.example.t.pc;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
    
        public static void main(String[] args) {
             List list = new ArrayList();
    
             new Thread(() -> {
                P p = new P(list);
                p.add();
            }).start();
    
            new Thread(()->{
                C c = new C(list);
                c.sub();
            }).start();
        }
    
    
    
    
    }
  • 相关阅读:
    第07组 Beta冲刺(3/5)
    第07组 Beta冲刺(2/5)
    第07组 Beta冲刺(1/5)
    第07组 Alpha事后诸葛亮
    第07组 Alpha冲刺(6/6)
    第07组 Alpha冲刺(5/6)
    【置顶】CSP/NOIP 2020 (高二游记)
    【置顶】CSP/S 2019退役祭
    【置顶】一些关于停课的感想(随时更新,连着前一篇博客)11/11~11/15
    【置顶】我的2018~2019信息学之路
  • 原文地址:https://www.cnblogs.com/smileblogs/p/11676499.html
Copyright © 2020-2023  润新知