Java 线程同步(wait、notify、notifyAll)
2016-07-09 01:30
543 查看
一、方法介绍
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(){}
输出
(2)不加synchronized(){}
2、同步方法测试
(1)synchronized方法前缀
输出
(2)不synchronized方法前缀
输出
3、使用其它锁对象测试
输出:
4、wait、notify进行线程同步
(1)一(wait)对一(notify)模式
输出
(2)多(wait)对一(notify)模式
5、notifyAll
三、总结:
(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()
这三个方法是等价的。
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() { @Override public 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() { @Override public 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() { @Override public 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() { @Override public 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() { @Override public 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() { @Override public 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() { @Override public 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() { @Override public 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 泛型 通配符类型
- Java的位运算符详解实例——与(&)、非(~)、或(|)、异或(^)
- rip协议java模拟实现
- java lang NoClassDefFoundError org springframework util MimeType
- Java中巧用字符串连接
- Java使用Tess4J 进行图片文字识别
- JAVA第二课——事件监听以及画板的制作
- java ZXing生成二维码及条码实例分享
- Eclipse中maven项目的三种启动方式
- 常见排序之Java实现
- java企业级通用权限安全框架源码
- javaEE 后台框架 SpringMVC Mybatis
- jasypt结合spring对数据库密码加密
- Java 读取文件内容
- Spring 3整合Quartz 2实现定时任务一:常规整合
- Spring 3整合Quartz 2实现定时任务二:动态添加任务
- Spring 3整合Quartz 2实现定时任务三:动态暂停 恢复 修改和删除任务
- Spring 整合Quartz 2实现定时任务四:细化调整及一些已知的问题
- Java Windows 环境配置
- Eclipse Git使用