wait和notify

来源:互联网 发布:新加坡读研费用 知乎 编辑:程序博客网 时间:2024/06/06 10:49
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锁    }}



0 0