会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132772个问题
Python 全系列/第十四阶段:Python 爬虫开发/爬虫基础(旧) 37486楼
Python 全系列/第一阶段:Python入门/编程基本概念 37487楼
Python 全系列/第十一阶段:重量级Web框架-Django/Django中级 37488楼
Python 全系列/第一阶段:Python入门/序列 37489楼

class ManTou {
    private int id;

    public ManTou(int id) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }
}

/**
 * 定义缓冲区类
 */
class SyncStack {
    //定义存放馒头的盒子
    private ManTou[] manTous = new ManTou[10];
    //定义操作盒子的索引
    private int index;

    /**
     * 放馒头的方法
     */

    public synchronized void put(ManTou manTou) {
        //判断盒子是否已满
        while (this.index == this.manTous.length) {
            try {
                this.wait();    //wait()必须在synchronized块中调用  wait()执行后 线程会将持有的对象锁释放 并进入阻塞状态 其他需要该对象锁的线程就可以继续运行了
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //唤醒取馒头的线程
        this.notify();      //该方法必须在synchronized块中调用 会唤醒处于等待状态队列中的一个线程
        this.manTous[this.index] = manTou;
        this.index++;
    }

    /**
     * 取馒头的方法
     */
    public synchronized ManTou get() {
        //判断盒子是否已空
        while (this.index == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.index--;
        return this.manTous[this.index];
    }
}

/**
 * 定义生产者线程类
 */
class ShengChan extends Thread {
    private SyncStack syncStack;

    public ShengChan(SyncStack syncStack) {
        this.syncStack = syncStack;
    }
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            ManTou manTou = new ManTou(i);
            syncStack.put(manTou);
            System.out.println("生产第"+(i+1)+"个馒头");
        }
    }
}

/**
 * 定义消费者线程类
 */
class Xiaofei extends Thread {
    private SyncStack syncStack;

    public Xiaofei(SyncStack syncStack) {
        this.syncStack = syncStack;
    }
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            ManTou manTou = this.syncStack.get();
            System.out.println("消费了" + (i+1) + "个馒头");

        }
    }
}

public class ProduceThread {
    public static void main(String[] args) {
        SyncStack syncStack = new SyncStack();
        new ShengChan(syncStack).start();
        new Xiaofei(syncStack).start();
    }
}

image.png

image.png

image.png

image.png

老师为什么我这里会出现先消费的现象啊? 

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 37490楼
Python 全系列/第十二阶段:Python_Django3框架/Django进阶 37491楼
Python 全系列/第一阶段:Python入门/函数和内存分析 37492楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 37493楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 37498楼

public class User3 {
    int id;        //id
    String name;   //账户名
    String pwd;   //密码
    static String company; //公司名称
    static {
        System.out.println("执行类的初始化工作");
        company = "北京尚学堂";
        printCompany();
    }  
    public static void printCompany(){
        System.out.println(company);
    }  
    public static void main(String[] args) {
        User3  u3 = new User3();
    }
}


问题1   我记得前面视频好像说过一个程序里只能有一个public定义的类啊。为什么这个程序里有三个public?难道是类里面可以用public 来修饰方法?

------------------------------------------------------------------------------------------------------

/**
 * 测试static关键字的用法
 * @author 高淇
 *
 */
public class User2 {
    int id; // id
    String name; // 账户名
    String pwd; // 密码
     
    static String company = "北京尚学堂"; // 公司名称
     
     
    public User2(int id, String name) {
        this.id = id;
        this.name = name;
    }
     
    public void login() {
        printCompany();
        System.out.println(company); 
        System.out.println("登录:" + name);
    }
     
    public static void printCompany() {
//         login();//调用非静态成员,编译就会报错
        System.out.println(company);
    }
     
    public static void main(String[] args) {
        User2 u = new User2(101, "高小七");
        User2.printCompany();
        User2.company = "北京阿里爷爷";
        User2.printCompany();
    }
}

问题二   为什么这个User修饰的类定义了一个 u 对象后 是

User2.printCompany();
        User2.company = "北京阿里爷爷";
        User2.printCompany();

我觉得应该用 u吧,就像这样:(PS:难道是因为静态方法可以直接用类名进行调用?)

u.printCompany();
        u.company = "北京阿里爷爷";
        u.printCompany();


JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 37499楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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