黑马程序员技术交流社区

标题: 运行程序遇到的问题 [打印本页]

作者: xiaogougou    时间: 2015-3-20 15:45
标题: 运行程序遇到的问题
求各位大神解答,郁闷中。

QQ图片20150320154450.png (4.42 KB, 下载次数: 5)

QQ图片20150320154450.png

作者: 殷俊    时间: 2015-3-20 16:08
把代码贴出来看看呗
作者: xiaogougou    时间: 2015-3-20 16:15
殷俊 发表于 2015-3-20 16:08
把代码贴出来看看呗

class Res
{
        String name;
        String sex;
}
class Input implements Runnable
{
        private Res r;
        Object obj=new Object();
         
        Input(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                int x=0;
                while(true)
                {        synchronized(obj)
                        {       
                                if(r.flag)
                                        wait();
                                if (x==0)
                                {
                                        r.name="Mike";
                                        r.sex="man";
                                }
                                else
                                {
                                        r.name="丽丽";
                                        r.sex="女女女女女";
                                }
                                x=(x+1)%2;
                                r.flag=true;
                                notify();
                        }
                }
        }
}
class Output implements Runnable
{
        private Res r;
        Object obj=new Object();
        Output(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                while(true)
                {
                        synchronized(obj)
                        {
                                if (!r.flag)
                                        wait();
                                System.out.println(r.name+"......"+r.sex);
                                r.flag=false;
                                notify();
                        }
                }
        }
}
class InputOutputDemo
{
        public static void main(String[] args)
        {
                Res r=new Res();
                Input in=new Input(r);
                Output out=new Output(r);
                Thread t1=new Thread(in);
                Thread t2=new Thread(out);
                t1.start();

                t2.start();
        }
}

作者: 董志立    时间: 2015-3-20 16:29
xiaogougou 发表于 2015-3-20 16:15
class Res
{
        String name;

有可能是那个编译器的问题。
作者: 殷俊    时间: 2015-3-20 16:44
xiaogougou 发表于 2015-3-20 16:15
class Res
{
        String name;

怎么没有定义flag的初始化值?
作者: xiaogougou    时间: 2015-3-20 17:48
董志立 发表于 2015-3-20 16:29
有可能是那个编译器的问题。

好像Thread类,好郁闷
作者: xiaogougou    时间: 2015-3-20 17:49
董志立 发表于 2015-3-20 16:29
有可能是那个编译器的问题。

好像少了Thread类
继承或者实现不了 Thread类
真麻烦
绝大多数程序在我这里运行不了。。。求解。。。。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2