先放上代码:
- 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同步
|
|