黑马程序员技术交流社区
标题:
notify()唤醒线程后接着代码块执行
[打印本页]
作者:
Shey
时间:
2015-3-16 15:30
标题:
notify()唤醒线程后接着代码块执行
先放上代码:
package temp;
class NotifyDemoTest implements Runnable {
public boolean flag = true;
@Override
public void run() {
synchronized (this) {
System.out.println("A");
if (flag)
try {
flag = false;
this.wait();
} catch (Exception e) {
}
else {
flag = true;
this.notify();
}
System.out.println("B");
}
}
}
public class NotifyDemo {
public static void main(String[] args) {
NotifyDemoTest ndt = new NotifyDemoTest();
new Thread(ndt).start();
new Thread(ndt).start();
}
}
结果:
A
A
B
B
复制代码
就不用我多说了吧,notify唤醒线程后,线程接着原来代码执行到的地方继续执行,不会重新开始
csdn同步
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2