在这里插入代码片
package com.d04.www;
/*
* 等待唤醒案例:线程之间的通信
* wait,notify方法,只能唤醒一个进程
* notifyAll()唤醒所有的进程,拥有多个顾客
* */
public class Example_4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//连接通信所用的锁对象,可以调用wait,notify()方法
final Object obj=new Object();
new Thread(){
public void run(){
while(true){
synchronized(obj){
System.out.println("顾客要包子!!");
try {
obj.wait();
//obj.wait(5000);没有老板进程时,没有被唤醒,有毫秒值将会被自动唤醒
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("包子以好,开吃!!!");
}
}
}
}.start();
new Thread(){
public void run(){
while(true){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized(obj){
System.out.println("顾客需等待5秒");
obj.notify();
}
}
}
}.start();
}
}
07-14
07-14
07-13
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交