黑马程序员技术交流社区
标题:
问个关于唤醒的小问题
[打印本页]
作者:
安浦鑫
时间:
2012-5-4 20:21
标题:
问个关于唤醒的小问题
private int turn=0; //FINAL 对STATIC变量有何影响
public synchronized void print1() {
if ( turn%3!=0)
try
{this.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 3; i++)
System.out.println("A");
System.out.println();
turn ++;
this.notify();
}
public synchronized void print2() {
if ( turn%3!=1)
try
{this.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 3; i++)
System.out.println("B");
System.out.println();
turn++;
this.notify();
}
复制代码
请问下.notify();方法运行过后,是从哪条语句开始运行?
作者:
云惟桉
时间:
2012-5-4 20:25
楼主这里用this唤醒的本方线程吧,那就是从wait的地方开始唤醒的。
执行的是wait之后的语句,唤醒的线程不会再进行 if 的判断了。
个人理解。
作者:
任睦强
时间:
2012-5-4 20:55
遇到notify(),运行系统立刻从等待池中任选一wait的线程进入寻找对象锁状态,该线程会重新找回对象锁继续执行该方法。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2