wait和notify

本文通过一个Java案例展示了如何使用synchronized关键字实现线程间的同步操作。案例中定义了一个Person类作为共享资源,并创建了两个线程:输入线程(Input)负责修改Person对象的名字属性,输出线程(Output)则读取并打印名字属性。通过wait和notify方法协调两个线程的工作流程。

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

public class Input implements Runnable {
    private Person person;
    private int nameIndex;
    public Input(Person person) {
        this.person = person;
    }

    public void run() {
        synchronized (person) {
            while (true) {
                if (person.isFlag()) {
                    try {
                        person.wait();
                        System.out.println("input receive output notify");
                    } catch (InterruptedException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }
                person.setName("name:"+nameIndex++);
                System.out.println("input:name " + person.getName());
                person.setFlag(true);
                person.notify();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

            }

        }
    }
}



public class Output implements Runnable {
    private Person person;

    public Output(Person person) {
        this.person = person;
    }

    public void run() {
        synchronized (person) {
            while (true) {
                if (!person.isFlag()) {
                    try {
                        person.wait();
                        System.out.println("output receive intput notify");
                    } catch (InterruptedException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }
                System.out.println("output:name " + person.getName());

                person.setFlag(false);
                person.notify();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

            }
        }
    }
}

public class Person {
    private String name;
    private boolean flag;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Person person = new Person();
        Input input = new Input(person);
        Output output = new Output(person);
        Thread thread1 = new Thread(input);
        Thread thread2 = new Thread(output);

        thread1.start();
        thread2.start();

        //thread1先运行到  synchronized (person) ,取得对象锁,thread2阻塞,thread1设置name=  "name:0"   ,flag = true
        //thread1调用notify,此时等待队列中没有线程
        // thread1调用person.wait(),释放 synchronized锁,进入等待队列,此时thread2获取synchronized锁,
        // thread2打印name   ,设置flag = false  ,调用notify,此时等待队列中的thread1,被唤醒,打印“input receive output notify” 没有获取synchronized锁
        //thread2 调用person.wait(), 释放 synchronized锁,进入等待队列,此时thread1获取synchronized锁


    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值