A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ELine 中级黑马   /  2015-1-9 00:03  /  975 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Produces {
        private int NumOfProduces;
        private boolean flag;
        private Object object;

        public synchronized void setNumOfproduces() {
                NumOfProduces++;
                System.out.println(Thread.currentThread().getName() + "生产了产品,产品数目为:"
                                + NumOfProduces);
                notify();
        }

        public synchronized void getNumOfproduces() {
                if (NumOfProduces <= 0) {
                        try {
                                wait();
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
                NumOfProduces--;
                System.out.println(Thread.currentThread().getName() + "消费了产品,剩余产品数目为:"
                                + NumOfProduces);
        }
}
getNumOfproduces和setNumOfproduces是同一个锁吗


3 个回复

倒序浏览
是同一个锁
回复 使用道具 举报
是的,都是this
回复 使用道具 举报
顶一下,来看看
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马