Java线程合作通讯

博客主要围绕Java线程合作与通信展开,介绍了wait()方法使线程等待并释放锁,notify()方法唤醒等待线程。还阐述了解决线程通信的方式,即生产者将东西放缓冲区,消费者从缓冲区拿取,二者互不干涉,通过flag完成进程的等待和唤醒。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程合作线程通信

wait(); 线程等待 放锁

notify(); 唤醒一个处于等待状态的线程

 

解决方式:

  • 生产着 把东西放在缓冲区

  • 消费者从缓冲区拿东西

  • 生产者和消费着之间不干涉

//利用缓存区
public class TestPC {
    public static void main(String[] args) {
        Container container = new Container();
        new Producer(container).start();
        new Consumer(container).start();
    }
}
//生产者
class Producer extends Thread{
    Container container;
    public Producer(Container container){
        this.container =container;
    }
​
    //生产
​
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
​
            container.push(new Production(i));
            System.out.println("做好了 "+ (i+1) +"个");
        }
    }
}
//消费者
class Consumer extends Thread{
    Container container;
    public Consumer(Container container){
        this.container =container;
    }
​
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("消费了 " + (container.pop().id+1)+" 个");
        }
    }
}
//商品
class Production{
    int id;
​
    public Production(int id) {
        this.id = id;
    }
}
//缓冲区
class Container{
    //容器大小
    Production[] productions = new Production[10];
    //计数
    int count =0 ;
    //生产者放入产品
    public synchronized void push(Production production){
        //如果容器满了 需要等待消费者
        if (count==productions.length){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //如果没有满 放入容器
        productions[count]=production;
        count++;
        //通知可以消费了
        this.notifyAll();
    }
​
    //消费者拿走产品
    public synchronized Production pop(){
        if (count==0){
            //等待生产者生产 消费者等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        count--;
        Production production = productions[count];
        //消费完了
        this.notifyAll();
        return production;
    }
​
}

用flag 来完成 进程的等待和唤醒

public class TestPcFlag {
    public static void main(String[] args) {
        PlatForm platForm = new PlatForm(); //创建一个PlatForm对象
​
        new Shower(platForm).start(); //将对象丢入线程构造器
        new Watcher(platForm).start(); //将对象丢入线程构造器
    }
}
​
class Shower extends Thread{
​
    PlatForm platForm;
    Shower(PlatForm platForm){ //构造器
        this.platForm =platForm;
    }
    @Override
    public void run(){ //重写run方法 运行10此 Platform类里的show方法
        for (int i = 0; i < 10; i++) {
            platForm.show(); //运行丢入Platform类里的show方法
        }
    }
}
​
​
class Watcher extends Thread{
    PlatForm platForm;
    Watcher(PlatForm platForm){ //构造器接受丢进来的 PlatForm 类
        this.platForm =platForm;
    }
    @Override
    public void run(){
        for (int i = 0; i < 10; i++) {//重写run方法 运行10此 Platform类里的watch方法
            platForm.watch(); //运行丢入Platform类里的watch方法
        }
    }
}
class PlatForm {
​
    boolean flag =true; //设定一个flag 来判断 进程是否需要等待
​
    public synchronized void show(){
        if (!flag){ //如果flag 时false
            try {
                this.wait(); //等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("表演节目");
​
        flag =!flag; //反转flag
        this.notifyAll(); //唤醒其他等待的进程
    }
​
​
    public synchronized void watch(){
        if (flag){ //如果flag 时ture
            try {
                this.wait(); //等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("观看节目");
        flag =!flag; //反转flag
        this.notifyAll();  //唤醒其他等待进程
    }
​
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值