会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132450个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/Lambda表达式(旧) 1381楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 1382楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 1383楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 1385楼

老师,同步代码块中 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 基础深化和提高/多线程技术(旧) 1386楼

老师,同步代码块中 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 基础深化和提高/多线程技术(旧) 1387楼

// 程序员奖金类
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 基础深化和提高/多线程技术(旧) 1388楼

老师,线程同步中,使用字符串作为锁对象为何就可以实现不同对象调用同一线程的同步?这种方式锁定的到底是什么?锁定的肯定不是创建的实例化对象吧,因为使用了不同的实例化对象去调用同一线程。

// 程序员类
class Programmers{
    private String name;

    public Programmers(String name) {
        this.name = name;
    }

    // 上洗手间
    public void wc () {
        // 以空字符串为锁对象
        synchronized (" ") {
            try {
                System.out.println(this.name + "打开卫生间门");
                Thread.sleep(3000);
                System.out.println(this.name + "上厕所");
                Thread.sleep(3000);
                System.out.println(this.name + "来也匆匆,去也冲冲");
                Thread.sleep(4000);
                System.out.println(this.name + "打开卫生间门");
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

// 上洗手间线程
class WC extends Thread {
    private Programmers pn;

    public WC(Programmers pn) {
        this.pn = pn;
    }

    @Override
    public void run() {
        this.pn.wc();
    }
}

public class ThreadConflictTest02 {

    public static void main(String[] args) {
        Programmers ps1 = new Programmers("张三");
        Programmers ps2 = new Programmers("李四");
        Programmers ps3 = new Programmers("王五");
        new WC(ps1).start();
        new WC(ps2).start();
        new WC(ps3).start();

    }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1390楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术(旧) 1393楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术(旧) 1394楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术(旧) 1395楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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