会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132452个问题
JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 16517楼
JAVA 全系列/第六阶段:项目管理与SSM框架/SpringMVC 16519楼

老师,同步代码块中 wait()方法及 notify() 方法是用 this 对象调用的,那这个对象到底是哪个类下的对象?是 缓冲区 BufferZone 类下实例化的对象嘛?

// 馒头类
class SteamBread {
    private int id;

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

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

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

    // 生产者方法馒头
    public synchronized void put(SteamBread steamBread) {
        while (this.index >= this.sb.length) {
            try {
                this.wait();   // this 引用当前类的实例,所以对应的对象为 BufferZone 类下实例化的对象 bz
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.sb[this.index] = steamBread;
        this.index++;
    }

    // 取馒头
    public synchronized SteamBread take() {
        while (this.index == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.index--;
        return this.sb[this.index];
    }
}

// 生产者线程类
class ProducerThread extends Thread {
    private BufferZone bz;

    public ProducerThread(BufferZone bz) {
        this.bz = bz;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; ++i) {
            System.out.println("生产馒头:" + i);
            SteamBread sb = new SteamBread(i);
            this.bz.put(sb);
        }
    }
}

// 消费者线程类
class ConsumerThread extends Thread {
    private BufferZone bz;

    public ConsumerThread(BufferZone bz) {
        this.bz = bz;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; ++i) {
            SteamBread sb = this.bz.take();
            System.out.println("消费馒头:" + i);
        }
    }
}


// 测试生产者消费者模式
public class ProducerConsumerModel {
    public static void main(String[] args) {
        BufferZone bz = new BufferZone();
        new ProducerThread(bz).start();
        new ConsumerThread(bz).start();
    }

}


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

老师,同步代码块中 wait()方法及 notify() 方法是用 this 对象调用的,那这个对象到底是哪个类下的对象?是 缓冲区 BufferZone 类下实例化的对象嘛?

// 馒头类
class SteamBread {
    private int id;

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

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

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

    // 生产者方法馒头
    public synchronized void put(SteamBread steamBread) {
        while (this.index >= this.sb.length) {
            try {
                this.wait();   // this 引用当前类的实例,所以对应的对象为 BufferZone 类下实例化的对象 bz
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.sb[this.index] = steamBread;
        this.index++;
    }

    // 取馒头
    public synchronized SteamBread take() {
        while (this.index == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.index--;
        return this.sb[this.index];
    }
}

// 生产者线程类
class ProducerThread extends Thread {
    private BufferZone bz;

    public ProducerThread(BufferZone bz) {
        this.bz = bz;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; ++i) {
            System.out.println("生产馒头:" + i);
            SteamBread sb = new SteamBread(i);
            this.bz.put(sb);
        }
    }
}

// 消费者线程类
class ConsumerThread extends Thread {
    private BufferZone bz;

    public ConsumerThread(BufferZone bz) {
        this.bz = bz;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; ++i) {
            SteamBread sb = this.bz.take();
            System.out.println("消费馒头:" + i);
        }
    }
}


// 测试生产者消费者模式
public class ProducerConsumerModel {
    public static void main(String[] args) {
        BufferZone bz = new BufferZone();
        new ProducerThread(bz).start();
        new ConsumerThread(bz).start();
    }

}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 16521楼
JAVA 全系列/第五阶段:JavaWeb开发/XML技术(旧) 16522楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 16523楼
JAVA 全系列/第五阶段:JavaWeb开发/XML技术(旧) 16524楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Spring 16526楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask数据库 16527楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 16528楼

// 程序员奖金类
class ProgrammerBonus {
    private String name;

    public ProgrammerBonus(String name) {
        this.name = name;
    }
    // 领取奖金
    public void TakeBonus () {
        // 不仅可以用 ProgrammerBonus.class,用 Bonus.class 也行
        synchronized (Bonus.class) {
            try {
                System.out.println(this.name + "被领导表扬");
                Thread.sleep(3000);
                System.out.println(this.name + "拿到奖金");
                Thread.sleep(2000);
                System.out.println(this.name + "话不多说,一切尽在行动中");
                Thread.sleep(3000);
                System.out.println(this.name + "离开办公室");
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

// 领取奖金线程
class Bonus extends Thread {
    private ProgrammerBonus pb;

    public Bonus(ProgrammerBonus pb) {
        this.pb = pb;
    }

    @Override
    public void run() {
       this.pb.TakeBonus();
    }
}

public class ThreadConflictTest03 {

    public static void main(String[] args) {
        ProgrammerBonus pb1 = new ProgrammerBonus("张三");
        Bonus b1 =new Bonus(pb1);
        ProgrammerBonus pb2 = new ProgrammerBonus("李四");
        Bonus b2 = new Bonus(pb2);
        ProgrammerBonus pb3 = new ProgrammerBonus("王五");
        Bonus b3 = new Bonus(pb3);

        b1.start();
        b2.start();
        b3.start();
    }
}

老师,在上面代码中,用类对象作为锁对象,我试了下发现用 ProgrammerBouns.class 或 Bouns.class 都可以,这两个类对象用起来有啥区别嘛?


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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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