会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132484个问题
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 14821楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 14825楼

老师为什么实用if-else返回的结果是这样的

package com.bjsxt.thread.prosumer2;

/** 
 * <p>Title: TestProducer</p>  
 * <p>Description: 生产者模式</p>  
 * @author xiaoding
 * @date Jul 20, 2020  
 * @version 1.0 
 */
public class TestProducer implements Runnable{
    //功能属性
    private TestSupermarket supermarket;    //超市
    //构造方法
    public TestProducer(TestSupermarket supermarket) {
        this.supermarket = supermarket;
    }
    
    //定义线程体
    @Override
    public void run() {
        for (int i = 0;i<10;i++) {
            if (i % 2 != 0) {
                supermarket.set("旺仔", "小馒头");
            }else {
                supermarket.set("康师傅", "方便面");
            }
        }
    }
}

public class TestSupermarket {
    // 功能属性    
    private String brand;    //品牌
    private String name;    //名称
    private boolean flag;    //开关判断是否需要唤醒线程
    
    //构造方法
    public TestSupermarket() {}
    public TestSupermarket(String brand,String name) {
        this.brand = brand;
        this.name = name;
    }
    
    //get、set
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    //定义一个存储的方法
    public synchronized void set(String brand,String name) {
        if (flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else {
            this.setBrand(brand);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            this.setName(name);
            System.out.println("=========生产者产生了:==========" + this.getBrand() + this.getName());
            this.notify();
            flag = true;
        }    
    }
    //定义一个取值的方法
    public synchronized void get() {
        if (flag) {
            System.out.println("消费者取走了:=============" + this.getBrand() + this.getName());
            this.notify();
            flag = false;
        }else {
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

public class TestConsumer implements Runnable{
    //功能属性
    private TestSupermarket supermarket;    //超市
    //构造方法
    public TestConsumer(TestSupermarket supermarket) {
        this.supermarket = supermarket;
    }
    
    //定义线程体
    @Override
    public void run() {
        for (int i = 0;i<10;i++) {
            supermarket.get();
        }
    }
}

public class Test {
    public static void main(String[] args) {
        //创建超市类
        TestSupermarket sup = new TestSupermarket();
        //创建生产者类
        TestConsumer p = new TestConsumer(sup);
        //创建消费者类
        TestProducer p2 = new TestProducer(sup);
        
        //创建代理角色线程类,并启动线程
        new Thread(p).start();
        new Thread(p2).start();
    }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 14826楼
Python 全系列/第四阶段:函数式编程和核心特性/内存管理(旧) 14827楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 14828楼
Python 全系列/第十一阶段:重量级Web框架-Django/Redis的入门与应用(拓展) 14835楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637