ruijiege

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
package cn.jiedada.controller;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

public class MyTest {

    public static void main(String[] args) {

        MyTest myThread = new MyTest();
        new Thread(){
            @Override
            public void run() {
                //synchronized需要锁住调用该类的notify对象
                synchronized (myThread){
                    for (int i=0;i<10;i++){
                        //先唤醒在wait
                        myThread.notify();
                        System.out.println("a");
                        try {
                            myThread.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                synchronized (myThread){
                    for (int i=0;i<10;i++){
                        myThread.notify();
                        System.out.println("b");
                        try {
                            myThread.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }.start();
    }

}

 

posted on 2021-07-14 08:03  哦哟这个怎么搞  阅读(54)  评论(0编辑  收藏  举报