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
- synchronized和wait()/notify()
- wait和notify实例
- wait()和notify()
- wait和notify使用
- notify 和wait
- notify 和wait 2
- wait(),notify()和锁
- wait和notify
- 理解wait和notify
- synchronized和wait()/notify()
- wait和notify实例
- 正确理解wait 和 notify
- wait()和notify()/notifyAll()
- wait()和notify()、notifyAll()
- 多线程-notify和wait
- wait和notify区别
- java wait和notify
- notify()和wait()
- POJ 3130 How I Mathematician Wonder What You Are!
- Study Ant《在ANT中替换尖括号》
- DSP6000的几个简单优化技巧
- Android中自动跳转到系统设置界面
- 数字信号处理的学习资源
- wait和notify
- linux 内核版本号
- 大数据的免费资料分享
- MFC自绘控件学习总结
- 白话压缩感知(含Matlab代码)
- java虚拟机运行时数据区
- linux 内核模块的编译(用于驱动开发)
- OpenCV基础篇之绘图及RNG随机数对象
- Skinned Mesh原理解析和一个最简单的实现示例