Object的wait、notify和notifyAll
Obect的wait、notify 和 notifyAll是Object提供的同步方法,也就是所有对象都生而带来的方法,估计搞java的没有不知道这几个方法的。那么他究竟是怎么使用的呢?在此处记录一下自己的理解。
先上一个最最最简单的例子。
public class SynchronizedTest {
public static void main(String[] args) throws Exception {
Thread mt = new Thread(){
@Override
public void run() {
synchronized (this) {
System.out.println("开始阻塞啦");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("阻塞结束啦");
}
}
};
mt.start();
Thread.sleep(500);
synchronized (mt) {
mt.notify();
}
}
}
运行结果:
开始阻塞啦
阻塞结束啦
上面的例子中,wait和notify方法都是在synchronized代码体中执行的,如果没有经过synchronized修饰,直接使用则会抛出java.lang.IllegalMonitorStateException异常。
至于原因,jdk源码wait方法中的描述为:
* The current thread must own this object's monitor. The thread
* releases ownership of this monitor and waits until another thread
* notifies threads waiting on this object's monitor to wake up
* either through a call to the {@code notify} method or the
* {@code notifyAll} method. The thread then waits until it can
* re-obtain ownership of the monitor and resumes execution.
翻译过来:
当前线程必须拥有此对象监视器。该线程释放对此监视器的所有权并等待,直到其他线程通过调用 notify 方法,或 notifyAll 方法通知在此对象的监视器上等待的线程醒来。然后该线程将等到重新获得对监视器的所有权后才能继续执行。
总结过来就是:要想使用wait等一系列方法,必须拥有当前对象的监视器(很多地方称为监视器锁)。
那么什么是对象的监视器呢?
简单说监视器是java对象为实现同步操作的一种机制,使用javap查看上边例子的线程部分的反编译指令:
final class com.xxx.SynchronizedTest$1 extends java.lang.Thread {
com.xxx.SynchronizedTest$1();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Thread."<init>":()V
4: return public void run();
Code:
0: aload_0
1: dup
2: astore_1
3: monitorenter
4: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
7: ldc #3 // String 开始阻塞啦
9: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
12: aload_0
13: invokevirtual #5 // Method java/lang/Object.wait:()V
16: goto 24
19: astore_2
20: aload_2
21: invokevirtual #7 // Method java/lang/InterruptedException.printStackTrace:()V
24: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
27: ldc #8 // String 阻塞结束啦
29: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
32: aload_1
33: monitorexit
34: goto 42
37: astore_3
38: aload_1
39: monitorexit
40: aload_3
41: athrow
42: return
Exception table:
from to target type
12 16 19 Class java/lang/InterruptedException
4 34 37 any
37 40 37 any
}
13行和27行可以看到monitorenter和monitorexit两条指令,monitorenter是尝试获取monitor,如果成功则执行,不成功则阻塞等待。monitorexit是释放monitor。
那么如何拥有该对象的监视器呢?
jdk源码notify方法中列举了三种方法:
By executing a synchronized instance method of that object.
By executing the body of a {@code synchronized} statement
that synchronizes on the object.
For objects of type {@code Class,} by executing a
synchronized static method of that class.
翻译大概是:
通过执行此对象的同步实例方法。
通过执行在此对象上进行同步的 synchronized 语句的代码块。
对于 Class 类型的对象,可以通过执行该类的同步静态方法。
我理解的就是被synchronized 修饰的方法或代码块(如果是代码块,需要使用同一对象做为synchronized的参数,目的是为了获取相同的监视器)。结合上边反编译的线程匿名内部类指令可以看到,使用synchronized 修饰的代码会使用monitorenter指令获取监视器,wait和notify必须获得监视器才能正确执行。
下面列举一下针对文章开始实例的错误示范:
错误实例1
public class SynchronizedTest {
public static void main(String[] args) throws Exception {
Thread mt = new Thread(){
@Override
public void run() {
synchronized (this) {
System.out.println("开始阻塞啦");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("阻塞结束啦");
}
}
};
mt.start();
Thread.sleep(500);
// synchronized (mt) {
mt.notify();
// }
}
}
执行结果:
开始阻塞啦
Exception in thread "main" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at xxx
原因:
notify方法没有获得监视器。
错误实例2:
public class SynchronizedTest {
public static void main(String[] args) throws Exception {
Thread mt = new Thread(new Runnable() {
@Override
public void run() {
synchronized (this) {
System.out.println("开始阻塞啦");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("阻塞结束啦");
}
}
});
mt.start();
Thread.sleep(500);
synchronized (mt) {
mt.notify();
}
}
}
执行结果:
开始阻塞啦
(线程持续wait中。。。
原因:
这个例子是我最开始的写法,放在这里有点不太合适,因为他并不是wait和notify错误使用导致的问题,而是错误使用Runnable导致的,最终我还是决定放上来吧,防止有人也会一时想不明白。
例子中使用 new Runnable 创建了一个匿名内部类并作为构造参数传给new Thread,导致构造的对象mt和匿名内部类的this不是同一个对象。所以导致notify不起作用= =、
最新文章
- JqueryDataTable的使用(.Net平台)
- C++指针参数引用
- 证书过期-->;app审核提示90034证书错误
- Java for LeetCode 228 Summary Ranges
- 【hdu1394】Minimum Inversion Number
- Delphi 全面控制Windows任务栏
- 手把手教你图片转ASCII码图
- c# 文件同步服务器,iis 集群 ,代码同步(二)
- WPF自学入门(四)WPF路由事件之自定义路由事件
- 用Jmeter实现mysql数据库的增删查改
- 线性回归和Logistic回归
- urllib、urllib2、urllib3区别和使用
- docker image 详解
- C语言求n的阶乘(n!)
- 关于tpg例程的仿真
- sudo安装某一文件报错:E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用) E: 无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它?
- 高程三 BOM 读书笔记
- GCC基础知识学习
- 技术分享会:深度学习Deep Leanring
- 【Java知识点专项练习】之 Java鲁棒性的特点