进阶day06-等待唤醒机制的API
This commit is contained in:
21
javaSE-day06/src/com/inmind/wait_notify08/Demo08.java
Normal file
21
javaSE-day06/src/com/inmind/wait_notify08/Demo08.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.inmind.wait_notify08;
|
||||
/*
|
||||
等待唤醒中的方法
|
||||
void wait() 导致当前线程等待,直到另一个线程调用该对象的 notify()方法或 notifyAll()方法。
|
||||
void notify() 唤醒正在等待对象监视器(锁对象)的单个线程。
|
||||
void notifyAll() 唤醒正在等待对象监视器(锁对象)的所有线程。
|
||||
|
||||
注意:只有在同步代码块中才有锁对象,只有锁对象才能调用以上api,否则报非法监视器状态异常
|
||||
一定是同步代码块或者同步方法中才有锁对象
|
||||
*/
|
||||
public class Demo08 {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
//创建一个java对象
|
||||
Object o = new Object();
|
||||
synchronized (o){//此时o才是锁对象
|
||||
o.wait();//让当前线程(主线程)无限等待
|
||||
}
|
||||
|
||||
System.out.println("程序结束");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user