黑马程序员技术交流社区

标题: 问个关于唤醒的小问题 [打印本页]

作者: 安浦鑫    时间: 2012-5-4 20:21
标题: 问个关于唤醒的小问题
  1.         private int turn=0;  //FINAL 对STATIC变量有何影响
  2.        
  3.         public synchronized  void print1() {
  4.                
  5.                 if ( turn%3!=0)
  6.                         try
  7.                 {this.wait();
  8.                 }
  9.                 catch (InterruptedException e) {
  10.                         e.printStackTrace();
  11.                 }
  12.                 for (int i = 0; i < 3; i++)
  13.                         System.out.println("A");
  14.                 System.out.println();
  15.                
  16.                 turn ++;                               
  17.                 this.notify();
  18.        
  19.         }
  20. public synchronized void print2() {
  21.                
  22.                 if ( turn%3!=1)
  23.                         try
  24.                 {this.wait();
  25.                 }
  26.                 catch (InterruptedException e) {
  27.                         e.printStackTrace();
  28.                 }
  29.                 for (int i = 0; i < 3; i++)
  30.                         System.out.println("B");
  31.                 System.out.println();
  32.                
  33.                 turn++;                               
  34.                 this.notify();
  35.         }
复制代码
请问下.notify();方法运行过后,是从哪条语句开始运行?
作者: 云惟桉    时间: 2012-5-4 20:25
楼主这里用this唤醒的本方线程吧,那就是从wait的地方开始唤醒的。
执行的是wait之后的语句,唤醒的线程不会再进行 if 的判断了。
个人理解。
作者: 任睦强    时间: 2012-5-4 20:55
遇到notify(),运行系统立刻从等待池中任选一wait的线程进入寻找对象锁状态,该线程会重新找回对象锁继续执行该方法。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2