Loading

wait()方法写在while循环中可以在线程接到通知后再一次判断条件

wait()方法写在while循环中可以在线程接到通知后再一次判断条件

synchronized public String pop() {
        String returnValue = "";
        try {
            while(list.size()==0) {
                System.out.println("pop操作中的:"+Thread.currentThread().getName()+" 线程呈wait状态");
                this.wait(); // 这里wait()
            } // 接到notify()通知之后,还要走这段判断
            System.out.println("pop操作中的: "+Thread.currentThread().getName()+" 准备要remove了");
            returnValue = ""+list.get(0);
            list.remove(0);
            this.notify();
            System.out.println("pop="+list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnValue;
    }

 

posted @ 2018-02-15 07:14  stono  阅读(132)  评论(0编辑  收藏  举报