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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 魏亮 于 2012-9-14 20:19 编辑

package cn.itcast.thread;
class ProConDemo
{
        public static void main(String[] args)
        {
                Res r = new Res();
                new Thread(new Con(r)).start();
                new Thread(new Pro(r)).start();
        }
}
class Res
{
        private String name;
        private int count;
        boolean flag;
        public synchronized void set(String name)
        {
                if(flag)
                        try
                        {
                                this.wait();
                        }
                        catch (Exception e)
                        {
                        }
                this.name=name+"=="+count++;
                System.out.println(Thread.currentThread().getName()+"++producer++"+this.name);
                flag=true;
                this.notify();
        }
        public synchronized void get()
        {
                if(!flag)
                        try
                        {
                                this.wait();
                        }
                        catch (Exception e)
                        {
                        }
                System.out.println(Thread.currentThread().getName()+"--consumer--"+name);
                flag=false;
                this.notify();
        }
}
class Pro implements Runnable
{
        private Res r;
        Pro(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                while(true)
                {
               
                        r.set("++goods++");
                }
        }
}
class Con implements Runnable   报的是这行出错,具体看下图
{
        private Res r;
        Con(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                while (true)
                {
                        r.get();
                }
        }
}





评分

参与人数 1技术分 +1 收起 理由
王德升 + 1 赞一个!

查看全部评分

5 个回复

倒序浏览
class Cons implements Runnable {     //Con换成别的
        private Res r;

        Cons(Res r) {
                this.r = r;
        }

        public void run() {
                while (true) {
                        r.get();
                }
        }
}

对于你这块代码中的类Con不能用这个,你换成其它的名字就可以了。可能Con比较特殊吧,我觉得这得Con被定义成类时和某些文件是有关联的,所以才会提示缺少文件不能被写入。
回复 使用道具 举报
本帖最后由 寇龙飞 于 2012-9-14 20:11 编辑
  1. package com.itheima.test;

  2. class Con { }
复制代码
代码简化如上,跟你的错误一样。

原因是,Con为windows下的保留字(虚拟设备名称),文件夹文件名都不能以此命名,com1、com2也是。所以,无法创建Con.java文件。也就不能正常使用。

要将此文件命名为Con.java时eclipse提示错误。



你的Con类只能换个名字了。
回复 使用道具 举报
进一步查官方文档,有解释:
Do not use the following reserved device names for the name of a file:

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.

Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended

回复 使用道具 举报
原来如此,谢谢上面几位,我知道了
回复 使用道具 举报
寇龙飞 发表于 2012-9-14 20:05
代码简化如上,跟你的错误一样。

原因是,Con为windows下的保留字(虚拟设备名称),文件夹文件名都不能以 ...

多谢指教
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马