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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 冒牌高手 于 2014-8-14 15:06 编辑

源代码如下,错误提示在图片:
class ProCon
{
        public static void main(String[] args)
        {
                //System.out.println("Hello World!");
                Resource r=new Resource ();
                Pro p=new Pro(r);
                Con c=new Con(r);


                Thread t0=new Thread(p);
                Thread t1=new Thread(c);

                t0.start();
                t1.start();
        }
}
class Resource
{
        private String name;
        private int num=1;
        private boolean flag=false;
        public synchronized void set(String name)
        {
                if (this.flag)
                {
                        try{this.wait();}catch (InterruptedException i){}        
                }
                        this.name=name+num;
                        num++;
                        System.out.println(Thread.currentThread().getName()+"..生产者...."+this.name);
                        this.flag=true;
                        notify();
               
                        
        }
        public synchronized void get()
        {
                if (!this.flag)
                {
                        try{this.wait();}catch (InterruptedException i){}               
                        
                }
                System.out.println(Thread.currentThread().getName()+"..消费者........."+this.name);
                this.flag=false;
                notify();
               
        }

}
class Pro implements Runnable
{
        Resource r;
        Pro (Resource r)
        {
                this.r=r;
        }
        public void run()
        {
                while (true)
                {
                        r.set("烤鸭");
                }
               
               
        }

}
class Con implements Runnable
{
        Resource r;
        Con (Resource r)
        {
                this.r=r;
        }
        public void run()
        {
                //r.get();
                while (true)
                {
                        r.get();
                }
               
        }
}

捕获.PNG (6.52 KB, 下载次数: 12)

怪异的错误提示,计算机还滴滴响

怪异的错误提示,计算机还滴滴响

4 个回复

倒序浏览
兄弟,保存了吗?
回复 使用道具 举报

保存了...没保存不是这个效果啊...
回复 使用道具 举报
冒牌高手 发表于 2014-8-1 14:01
保存了...没保存不是这个效果啊...

你这个错误是致命性的错误,我只有在网络编程和数据库编程时出现过,但看了你的代码,应该没有什么问题,找不出原因
回复 使用道具 举报
huangxuanheng 发表于 2014-8-1 20:52
你这个错误是致命性的错误,我只有在网络编程和数据库编程时出现过,但看了你的代码,应该没有什么问题, ...

多谢啦,我换了个电脑还是不行...代码跟教程上的比了下也没发现错误...好悲剧啊,哈哈
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马