Java 线程同步(wait、notify、notifyAll)

举报
福州司马懿 发表于 2021/11/19 04:19:09 2021/11/19
【摘要】 一、方法介绍 1、void wait() 使得线程进入等待状态,直到它被其他线程通过notify()或者notifyAll唤醒。该方法只能在同步方法(void synchronized methodName( args... ){ ... })或者 “同步块内部“ (synchronized(object){ ... })...

一、方法介绍

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(){}


  
  1. class A{}
  2. public static void main(String[] args) {
  3. A object = new A();
  4. System.out.println("Main Thread Id = " + Thread.currentThread().getId());
  5. synchronized (object) {
  6. try {
  7. System.out.println("This Thread Id = " + Thread.currentThread().getId());
  8. object.wait();
  9. } catch (InterruptedException e) {
  10. </span>e.printStackTrace();
  11. }
  12. }
  13. }

输出


(2)不加synchronized(){}


  
  1. class A{}
  2. public static void main(String[] args) {
  3. A object = new A();
  4. System.out.println("Main Thread Id = " + Thread.currentThread().getId());
  5. //synchronized (object) {
  6. try {
  7. </span>System.out.println("This Thread Id = " + Thread.currentThread().getId());
  8. </span>object.wait();
  9. } catch (InterruptedException e) {
  10. </span>e.printStackTrace();
  11. }
  12. //}
  13. }


2、同步方法测试

(1)synchronized方法前缀


  
  1. class A
  2. {
  3. public synchronized void test() throws InterruptedException {
  4. //synchronized (this) {
  5. System.out.println("Object Thread Id = " + Thread.currentThread().getId());
  6. wait();
  7. //}
  8. };
  9. }
  10. public static void main(String[] args) {
  11. A object = new A();
  12. System.out.println("Main Thread Id = " + Thread.currentThread().getId());
  13. //synchronized (object) {
  14. try {
  15. System.out.println("This Thread Id = " + Thread.currentThread().getId());
  16. //object.wait();
  17. object.test();
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. //}
  22. }

输出


(2)不synchronized方法前缀


  
  1. class A
  2. {
  3. public void test() throws InterruptedException {
  4. //synchronized (this) {
  5. System.out.println("Object Thread Id = " + Thread.currentThread().getId());
  6. wait();
  7. //}
  8. };
  9. }
  10. public static void main(String[] args) {
  11. A object = new A();
  12. System.out.println("Main Thread Id = " + Thread.currentThread().getId());
  13. //synchronized (object) {
  14. try {
  15. System.out.println("This Thread Id = " + Thread.currentThread().getId());
  16. //object.wait();
  17. object.test();
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. //}
  22. }

输出


3、使用其它锁对象测试


  
  1. class A{}
  2. public static void main(String[] args) {
  3. A object = new A();
  4. Object object2 = new Object();
  5. System.out.println("Main Thread Id = " + Thread.currentThread().getId());
  6. synchronized (object) {
  7. //synchronized (object2) {
  8. try {
  9. System.out.println("This Thread Id = " + Thread.currentThread().getId());
  10. object.wait();
  11. //object.test();
  12. } catch (InterruptedException e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. }

输出:


4、wait、notify进行线程同步

(1)一(wait)对一(notify)模式


  
  1. class A{}
  2. public static void main(String[] args) {
  3. final A object = new A();
  4. System.out.println("main thread id = " + Thread.currentThread().getId());
  5. new Thread(new Runnable() {
  6. @Override
  7. public void run() {
  8. synchronized (object) {
  9. try {
  10. System.out.println("before wait thread id = " + Thread.currentThread().getId());
  11. object.wait();
  12. System.out.println("after wait thread id = " + Thread.currentThread().getId());
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }
  18. }).start();
  19. new Thread(new Runnable() {
  20. @Override
  21. public void run() {
  22. try {
  23. Thread.sleep(1000);
  24. } catch (InterruptedException e) {
  25. e.printStackTrace();
  26. }
  27. synchronized (object) {
  28. System.out.println("before notify thread id = " + Thread.currentThread().getId());
  29. object.notify();
  30. System.out.println("after notify thread id = " + Thread.currentThread().getId());
  31. }
  32. }
  33. }).start();
  34. }

输出

(2)多(wait)对一(notify)模式


  
  1. class A{}
  2. public static void main(String[] args) {
  3. final A object = new A();
  4. System.out.println("main thread id = " + Thread.currentThread().getId());
  5. new Thread(new Runnable() {
  6. @Override
  7. public void run() {
  8. synchronized (object) {
  9. try {
  10. System.out.println("before wait1 thread id = " + Thread.currentThread().getId());
  11. object.wait();
  12. System.out.println("after wait1 thread id = " + Thread.currentThread().getId());
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }
  18. }).start();
  19. new Thread(new Runnable() {
  20. @Override
  21. public void run() {
  22. synchronized (object) {
  23. try {
  24. System.out.println("before wait2 thread id = " + Thread.currentThread().getId());
  25. object.wait();
  26. System.out.println("after wait2 thread id = " + Thread.currentThread().getId());
  27. } catch (InterruptedException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32. }).start();
  33. new Thread(new Runnable() {
  34. @Override
  35. public void run() {
  36. try {
  37. Thread.sleep(1000);
  38. } catch (InterruptedException e) {
  39. e.printStackTrace();
  40. }
  41. synchronized (object) {
  42. System.out.println("before notify thread id = " + Thread.currentThread().getId());
  43. object.notify();
  44. System.out.println("after notify thread id = " + Thread.currentThread().getId());
  45. }
  46. }
  47. }).start();
  48. }

5、notifyAll


  
  1. class A{}
  2. public static void main(String[] args) {
  3. final A object = new A();
  4. System.out.println("main thread id = " + Thread.currentThread().getId());
  5. new Thread(new Runnable() {
  6. @Override
  7. public void run() {
  8. synchronized (object) {
  9. try {
  10. System.out.println("before wait1 thread id = " + Thread.currentThread().getId());
  11. object.wait();
  12. System.out.println("after wait1 thread id = " + Thread.currentThread().getId());
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }
  18. }).start();
  19. new Thread(new Runnable() {
  20. @Override
  21. public void run() {
  22. synchronized (object) {
  23. try {
  24. System.out.println("before wait2 thread id = " + Thread.currentThread().getId());
  25. object.wait();
  26. System.out.println("after wait2 thread id = " + Thread.currentThread().getId());
  27. } catch (InterruptedException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32. }).start();
  33. new Thread(new Runnable() {
  34. @Override
  35. public void run() {
  36. try {
  37. Thread.sleep(1000);
  38. } catch (InterruptedException e) {
  39. e.printStackTrace();
  40. }
  41. synchronized (object) {
  42. System.out.println("before notify thread id = " + Thread.currentThread().getId());
  43. object.notifyAll();
  44. System.out.println("after notify thread id = " + Thread.currentThread().getId());
  45. }
  46. }
  47. }).start();
  48. }


三、总结:

(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()

这三个方法是等价的。


文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/chy555chy/article/details/51864773

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。