会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132440个问题
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 18811楼
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 18812楼

/**
 * 账户类
 */
class Account1{
    // 账户
    private String account1;

    // 账户余额
    private double balance;

    public Account1() {
    }

    public Account1(String account1, double balance) {
        this.account1 = account1;
        this.balance = balance;
    }

    public String getAccount1() {
        return account1;
    }

    public void setAccount1(String account1) {
        this.account1 = account1;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}

/**
 * 取款线程
 */
class DrawThread1 extends Thread{

    // 账户对象
    private Account1 account1;

    // 取款金额
    private double drawMoney;
    
    public DrawThread1(String name,Account1 account1,double drawMoney){
        super(name);
        this.account1 = account1;
        this.drawMoney = drawMoney;
    }
    /**
     * 取款线程的线程体
     */
    @Override
    public void run() {
        // 使用synchronized块实现线程同步
        synchronized (this.account1) { // 当前账户对象作为线程锁
            if(this.account1.getBalance() >= this.drawMoney){
                System.out.println("取款成功,吐出钞票:"+this.drawMoney);
                try {
                    Thread.sleep(1000); // 线程休眠1s
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 更新账户余额
                this.account1.setBalance(this.account1.getBalance() - this.drawMoney);
                System.out.println("账户余额:"+this.account1.getBalance());
            } else {
                System.out.println("取款失败,账户余额不足");
            }
        }
    }
}
/**
 * 取款
 */
public class DrawMoneyThread2 {

    public static void main(String[] args) {

        // 实例化账户对象
        Account1 account1 = new Account1("1234",1000);

        // 启动线程
        new DrawThread1("老公",account1,800).start();
        new DrawThread1("老婆",account1,800).start();

    }
}

image.png

为什么线程名没有设置成功?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 18813楼
Python 全系列/第一阶段:Python入门/序列 18815楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 18817楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 18818楼
JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 18819楼
JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 18822楼
JAVA 全系列/第五阶段:JavaWeb开发/Ajax技术详解(旧) 18823楼

package G_Multithreading.E_Synchronized;

//面包类
class Bread{
    private int id;
    public Bread(int id){
        this.id = id;
    }
}

//缓冲区
class SynBuffer{
    //存放面包的盒子
    private Bread[] breads = new Bread[10];
    //存放面包盒子的索引
    private int index;
    //放面包
    public synchronized void push(Bread bread){
        //判断盒子是否存满
        while (breads.length == this.index + 1){
            try {
                /*
                语法:wait(),该方法必须要在synchronized块中调用。
                     wait执行后,线程会将持有的对象锁释放,并进入阻塞状态,
                     其他需要该对象锁的线程就可以继续运行了。
                 */
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            /*
            语法:该方法必须要在synchronized块中调用。
                 该方法会唤醒处于相同对象的,并且等待状态队列中的一个线程。
             */
            //提醒取面包
            this.notify();
            breads[this.index] = bread;
            this.index++;
        }
    }
    //取走面包
    public synchronized Bread pop(){
        while (breads.length == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.index--;
        return breads[this.index];
    }
}

//生产面包线程
class Produce extends Thread{
    SynBuffer synBuffer;
    public Produce(SynBuffer synBuffer){
        this.synBuffer = synBuffer;
    }
    @Override
    public void run() {
        for (int i = 0; i < 10; i++){
            Bread bread = new Bread(i);
            this.synBuffer.push(bread);
            System.out.println("生产面包" + i);
        }
    }
}

//消费者线程
class Customer extends Thread{
    SynBuffer synBuffer;
    public Customer(SynBuffer synBuffer){
        this.synBuffer = synBuffer;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++){
            Bread bread = this.synBuffer.pop();
            System.out.println("取走面包" + i);
        }
    }
}
public class SynchronizedBuffer {
    public static void main(String[] args) {
        SynBuffer synBuffer = new SynBuffer();
        new Produce(synBuffer).start();
        new Customer(synBuffer).start();
    }
}

老师,他到取出的是够就高速index是-1,哪里出错了?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 18825楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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