使用wait()与notify()改写上面的例子
来源:互联网 发布:linux 复制覆盖文件 编辑:程序博客网 时间:2024/05/23 16:11
package concurrency.wait;
import java.util.concurrent.TimeUnit;
class MyObject implements Runnable {
private Monitor monitor;
public MyObject(Monitor monitor) {
this.monitor = monitor;
}
public void run() {
try {
TimeUnit.SECONDS.sleep(3);
System.out.println("i'm going.");
monitor.gotMessage();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Monitor implements Runnable {
private volatile boolean go = false;
public synchronized void gotMessage() throws InterruptedException {
go = true;
notify();
}
public synchronized void watching() throws InterruptedException {
while (go == false)
wait();
System.out.println("He has gone.");
}
public void run() {
try {
watching();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Wait {
public static void main(String[] args) {
Monitor monitor = new Monitor();
MyObject o = new MyObject(monitor);
new Thread(o).start();
new Thread(monitor).start();
}
}
import java.util.concurrent.TimeUnit;
class MyObject implements Runnable {
private Monitor monitor;
public MyObject(Monitor monitor) {
this.monitor = monitor;
}
public void run() {
try {
TimeUnit.SECONDS.sleep(3);
System.out.println("i'm going.");
monitor.gotMessage();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Monitor implements Runnable {
private volatile boolean go = false;
public synchronized void gotMessage() throws InterruptedException {
go = true;
notify();
}
public synchronized void watching() throws InterruptedException {
while (go == false)
wait();
System.out.println("He has gone.");
}
public void run() {
try {
watching();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Wait {
public static void main(String[] args) {
Monitor monitor = new Monitor();
MyObject o = new MyObject(monitor);
new Thread(o).start();
new Thread(monitor).start();
}
}
i'm going.
He has gone.
- 使用wait()与notify()改写上面的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- 一个理解wait()与notify()的例子
- beanstalkc 的常用指令
- Java中四种引用(强、软、弱、虚)
- Linux shell编程 3 ---- shell中的判断
- ZOJ:2913 Bus Pass
- 消息队列RabbitMQ和ActiveMQ的生产者流量控制
- 使用wait()与notify()改写上面的例子
- 12组nodes MySQL DB,每组2台Master-Master,批量清除过期的binlog释放磁盘空间
- android软件创建快捷方式
- fedora centos,ubuntu关于进程资源限制的问题
- 简单的内存池分析
- 加入header的自定义GridView
- Apache——Rewrite重写规则
- 大数据量,海量数据 处理方法总结
- 2013 ACM/ICPC 长沙网络赛B题解题报告