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

© 戎石锁 中级黑马   /  2012-8-13 22:48  /  1528 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 戎石锁 于 2012-8-13 22:50 编辑

class InOutDemo
{
public static void main(String[] args)
{
  Res r= new Res();
  In in = new In(r);
  Out out=new Out(r);
  Thread t1 = new Thread(in);
  Thread t2 = new Thread(out);
  t1.start();
  t2.start();
}
}

class Res
{
String name;
String sex;
public boolean flag=false;
}
class In implements Runnable
{
private Res r;
In(Res r)
{
  this.r=r;
}
public void run()
{
  int x=0;
  while(true)
  {
   synchronized(In.class)
   {
    if(r.flag)
     try{r.wait();}catch(Exception e){}
    if(x==0)
    {
     r.name="第一线程";
     r.sex="第一线程   第一线程";
    }
    else
    {
     r.name="dierxiancheng";
     r.sex="dierxaincheng";
    }
    x=(x+1)%2;
    r.flag=true;
    r.notify();
   }
  }
}
}

class Out implements Runnable
{
        
        private Res r;
        Out(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                while(true)
                {
                        
                        synchronized(In.class)
                        {
                                if(!r.flag)
                                {
                                        try{r.wait();}catch (Exception e){} //In.wait();为什么不能用In,我已经定义了一个In类
                                System.out.println(Thread.currentThread().getName()+r.name+"   "+r.sex);
                                r.flag=false;
                                r.notify(); //In.notify();为什么不能用In,我已经定义了一个In类                                }
                        }
                }
        }问题是:
看老毕视频,说在等待唤醒机制中,wait()方法必须表示出它操作那个线程所属的锁,我的锁是In.class,
但是In.wait();编译无法通过
}

1 个回复

倒序浏览
class Res
{
        String name;
        String sex;
        boolean flag=false;
}

class Input implements Runnable
{
        private Res r;
        Input(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                int x=0;
                while(true)
                {
                        synchronized(r)
                        {
                                if(r.flag)
                                   wait();
                                if(x==0)
                                {
                                        r.name="mike";
                                        r.sex="man";
                                }
                                else
                                {
                                        r.name="lily";
                                        r.sex="girl";
                                }
                                x=(x+1)%2;
                                r.flag=true;
                                notify();//这里一定是唤醒out,因为在f为false的情况下都会读一遍这里,此时只有out处于wait状态,所以只唤醒out。                        }
                }
        }
}

class Output implements Runnable
{
        private Res r;
        Output(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                while(true)
                {
                        synchronized(r)//这里的对象是任意对象,只要是对象都可以,注意同一个同步内要用同一个对象                        {
                                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();
        }
}

这个notify是唤醒正在wait的线程,你现在只有两个线程如果你有过个线程的话,多个wait的话就不一定唤醒谁了,如果要唤醒多个线程可以用notifyAll,唤醒其他的,没有在notify中加标记这么一说。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马