Java 线程同步(wait、notify、notifyAll)
来源:互联网 发布:svm算法步骤 编辑:程序博客网 时间:2024/04/30 19:47
一、方法介绍
1、void wait()
使得线程进入等待状态,直到它被其他线程通过notify()或者notifyAll唤醒。该方法只能在同步方法(void synchronized methodName( args... ){ ... })或者 “同步块内部“ (synchronized(object){ ... })被调用。如果当前线程不是锁的持有者,该方法会抛出一个IllegalMonitorStateException异常。
即如果锁住的是object,那么你只能调用object的wait()方法。
2、void notify()
随机选择一个在该对象上调用wait方法的线程,解除其阻塞状态。该方法只能在同步方法(void synchronized methodName( args... ){ ... })或者 “同步块内部“ (synchronized(object){ ... })被调用。如果当前线程不是锁的持有者,该方法会抛出一个IllegalMonitorStateException异常。
3、void notifyAll()
解除所有那些在该对象上调用wait方法的线程的阻塞状态。该方法只能在同步方法(void synchronized methodName( args... ){ ... })或者 “同步块内部“ (synchronized(object){ ... })被调用。如果当前线程不是锁的持有者,该方法会抛出一个IllegalMonitorStateException异常。
二、范例讲解( 注意测试类A只能声明为外部类,下面代码在 class A 和 main方法 之间 省略了MainClass { ... } )
1、“同步块”测试
(1)加synchronized(){}
class A{}public static void main(String[] args) {A object = new A();System.out.println("Main Thread Id = " + Thread.currentThread().getId());synchronized (object) {try {System.out.println("This Thread Id = " + Thread.currentThread().getId());object.wait();} catch (InterruptedException e) {</span>e.printStackTrace();}}}
输出
(2)不加synchronized(){}
class A{}public static void main(String[] args) {A object = new A();System.out.println("Main Thread Id = " + Thread.currentThread().getId());//synchronized (object) {try {</span>System.out.println("This Thread Id = " + Thread.currentThread().getId());</span>object.wait();} catch (InterruptedException e) {</span>e.printStackTrace();}//}}输出如下,L24指的是object.wait()的那行
2、同步方法测试
(1)synchronized方法前缀
class A{public synchronized void test() throws InterruptedException {//synchronized (this) {System.out.println("Object Thread Id = " + Thread.currentThread().getId());wait();//}};}public static void main(String[] args) {A object = new A();System.out.println("Main Thread Id = " + Thread.currentThread().getId());//synchronized (object) {try {System.out.println("This Thread Id = " + Thread.currentThread().getId());//object.wait();object.test();} catch (InterruptedException e) {e.printStackTrace();}//}}
输出
(2)不synchronized方法前缀
class A{public void test() throws InterruptedException {//synchronized (this) {System.out.println("Object Thread Id = " + Thread.currentThread().getId());wait();//}};}public static void main(String[] args) {A object = new A();System.out.println("Main Thread Id = " + Thread.currentThread().getId());//synchronized (object) {try {System.out.println("This Thread Id = " + Thread.currentThread().getId());//object.wait();object.test();} catch (InterruptedException e) {e.printStackTrace();}//}}
输出
3、使用其它锁对象测试
class A{}public static void main(String[] args) {A object = new A();Object object2 = new Object();System.out.println("Main Thread Id = " + Thread.currentThread().getId());synchronized (object) {//synchronized (object2) {try {System.out.println("This Thread Id = " + Thread.currentThread().getId());object.wait();//object.test();} catch (InterruptedException e) {e.printStackTrace();}}}
输出:
4、wait、notify进行线程同步
(1)一(wait)对一(notify)模式
class A{}public static void main(String[] args) {final A object = new A();System.out.println("main thread id = " + Thread.currentThread().getId());new Thread(new Runnable() {@Overridepublic void run() {synchronized (object) {try {System.out.println("before wait thread id = " + Thread.currentThread().getId());object.wait();System.out.println("after wait thread id = " + Thread.currentThread().getId());} catch (InterruptedException e) {e.printStackTrace();}}}}).start();new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}synchronized (object) {System.out.println("before notify thread id = " + Thread.currentThread().getId());object.notify();System.out.println("after notify thread id = " + Thread.currentThread().getId());}}}).start();}
输出
(2)多(wait)对一(notify)模式
class A{}public static void main(String[] args) {final A object = new A();System.out.println("main thread id = " + Thread.currentThread().getId());new Thread(new Runnable() {@Overridepublic void run() {synchronized (object) {try {System.out.println("before wait1 thread id = " + Thread.currentThread().getId());object.wait();System.out.println("after wait1 thread id = " + Thread.currentThread().getId());} catch (InterruptedException e) {e.printStackTrace();}}}}).start();new Thread(new Runnable() {@Overridepublic void run() {synchronized (object) {try {System.out.println("before wait2 thread id = " + Thread.currentThread().getId());object.wait();System.out.println("after wait2 thread id = " + Thread.currentThread().getId());} catch (InterruptedException e) {e.printStackTrace();}}}}).start();new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}synchronized (object) {System.out.println("before notify thread id = " + Thread.currentThread().getId());object.notify();System.out.println("after notify thread id = " + Thread.currentThread().getId());}}}).start();}输出
5、notifyAll
class A{}public static void main(String[] args) {final A object = new A();System.out.println("main thread id = " + Thread.currentThread().getId());new Thread(new Runnable() {@Overridepublic void run() {synchronized (object) {try {System.out.println("before wait1 thread id = " + Thread.currentThread().getId());object.wait();System.out.println("after wait1 thread id = " + Thread.currentThread().getId());} catch (InterruptedException e) {e.printStackTrace();}}}}).start();new Thread(new Runnable() {@Overridepublic void run() {synchronized (object) {try {System.out.println("before wait2 thread id = " + Thread.currentThread().getId());object.wait();System.out.println("after wait2 thread id = " + Thread.currentThread().getId());} catch (InterruptedException e) {e.printStackTrace();}}}}).start();new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}synchronized (object) {System.out.println("before notify thread id = " + Thread.currentThread().getId());object.notifyAll();System.out.println("after notify thread id = " + Thread.currentThread().getId());}}}).start();}输出
三、总结:
(1)由1和2可以看出:object.wait()方法只能在同步方法(void synchronized methodName( args... ){ ... })或者 “同步块内部“ (synchronized(object){ ... })被调用,否则会抛出一个IllegalMonitorStateException异常。
(2)由3可以看出:object.wait()方法只能在锁住自己的情况下被调用,否则会抛出一个IllegalMonitorStateException异常。
(3)由4可得:一个notify只能唤醒一个wait的线程
(4)由5可得:一个notifyAll可以唤醒多个wait的线程
四、注意点:(网上很多博客都讲的不清不楚的,龙神我估计他们就没有花时间真正的去测试过)
wait、notify和notifyAll方法是Object类的final native方法。所以这些方法不能被子类重写,Object类是所有类的超类,因此在程序中有以下三种形式调用wait等方法:
1、wait()
2、this.wait()
3、super.wait()
这三个方法是等价的。
- Java 线程同步(wait、notify、notifyAll)
- 【java】线程同步(synchronized,wait,notify,notifyAll)
- java线程同步(synchronized,wait,notify,notifyAll)
- java线程wait、notify、notifyAll
- JAVA -- 线程wait()、notify()、notifyAll()
- JAVA线程 -- wait notify notifyAll
- java线程:wait,notify,notifyAll
- Java 线程同步互斥 wait、notify、notifyall
- 关于线程同步通信 wait() notify() notifyAll()
- wait notify notifyall 线程同步方法简述
- 线程同步之wait,notify,notifyall
- 线程同步notify,notifyall,wait探究
- 传统线程同步wait(),notify,notifyall()
- 线程wait-notify-notifyAll
- Java 线程wait, notify and notifyAll 示例
- Java线程中的wait, notify and notifyAll
- JAVA线程间协作:wait.notify.notifyAll
- java的线程通信wait(),notify(),notifyAll()
- java lang NoClassDefFoundError org springframework util MimeType
- 关于模型数组进行本地保存中遇到的一些问题
- CentOS更改yum源与更新系统
- 查询一个表中的全部字段
- spring security4 之 SSO
- Java 线程同步(wait、notify、notifyAll)
- 读书笔记:《算法导论》,第1章:算法在计算中的作用
- java初学之arrayList
- Linux中搭建一个ftp服务器详解
- Linux中搭建一个ftp服务器详解
- 黑马程序员-iOS-C语言-源程序编译执行流程
- 步行导航地图认知特点文献摘要
- 无题
- Two Sum II – Input array is sorted