这段代码可运行,是关于模拟车辆通过交通路口的程序,下面这段程序已经实现对车辆进行控制,不会发生碰撞,我看不太懂,请高手帮忙解答一下
import java.applet.Applet;
import java.awt.*;
public class Example7_8 extends Applet implements Runnable
{


Thread AnimThread=null;
ICar  LRcar,TBcar;
TrafficCop tCop;
   public void init()
{
      resize(400,400);
  tCop=new TrafficCop();
      LRcar=new ICar(tCop,ICar.leftToRight,10);
  TBcar=new ICar(tCop,ICar.topToBotton,17);
}
   public void start()
{
      if (AnimThread==null)
      {
  AnimThread=new Thread(this);
  AnimThread.start();
  if (LRcar!=null && TBcar!=null)
  {
  LRcar.start();
  TBcar.start();
  }
      }
  else
{
  AnimThread.resume();  //恢复线程运行
  if (LRcar!=null){LRcar.resume(); }
          if (TBcar!=null){TBcar.resume(); }
}
    }
   public void stop()
{
  AnimThread.suspend();  //挂起线程
  if (LRcar!=null){LRcar.suspend(); }
          if (TBcar!=null){TBcar.suspend(); }
}
   public void run()
{
   while(true)
{
   try{Thread.sleep(50);}
   catch(InterruptedException e){}
   repaint();
}
}
   public void paint(Graphics g)
{
   Color saveColor=g.getColor();
   g.setColor(Color.black);
   g.fillRect(0,180,400,40);
   g.fillRect(180,0,40,400);
   LRcar.drawCar(g);
   TBcar.drawCar(g);
}
   public void update(Graphics g)
{
   if(!isValid())
{
   paint(g);
   return;
}
   LRcar.drawCar(g);
   TBcar.drawCar(g);
}


}class ICar extends Thread
{
public int lastPos=-1;
    public int carPos=0;
public int speed=10;  //初始化小车速度 
public int direction=1;//初始化小车的行驶方向
public TrafficCop tCop;
public final static int leftToRight=1;
    public final static int topToBotton=2;
public ICar(TrafficCop tCop)
{
this(tCop,ICar.leftToRight,10);
}
public ICar(TrafficCop tCop, int direction, int speed)
{
this.tCop=tCop;
this.speed=speed;
this.direction=direction;
}
    public void run()
{
while(true)
{
tCop.checkAndGo(carPos,speed);
carPos+=speed;
if (carPos>=400)
  { carPos=0; }
            try{Thread.sleep(200);}
catch(InterruptedException e){}
}
}
  public void drawCar(Graphics g)         //绘制小车
{
  if(direction==ICar.leftToRight)     //方向判断
{
  if(lastPos>=0)                  //位置判断
{
  g.setColor(Color.black);
  g.fillRect(0+lastPos,185,40,32);
}
g.setColor(Color.gray);
g.fillOval(2+carPos,185,10,10);
            g.fillOval(26+carPos,185,10,10);
            g.fillOval(2+carPos,205,10,10);
            g.fillOval(26+carPos,205,10,10);
            g.setColor(Color.green);
            g.fillOval(0+carPos,190,40,20);
            lastPos=carPos;
}
else
{
if(lastPos>=0)
{
g.setColor(Color.black);
g.fillRect(185,0+lastPos,32,40);
}
g.setColor(Color.gray);
g.fillOval(185,2+carPos,10,10);
g.fillOval(185,26+carPos,10,10);
g.fillOval(205,2+carPos,10,10);
g.fillOval(205,26+carPos,10,10);
g.setColor(Color.yellow);
g.fillRect(190,0+carPos,20,40);
lastPos=carPos;
}
}
public void updateCar(Graphics g)  //更新
{
if (lastPos!=carPos)
{
drawCar(g);
}
}
}class TrafficCop   //该类用于ICar线程的控制
{
  private boolean IntersectionBusy=false;
  //同步化方法
  public synchronized void checkAndGo(int carPos,int speed)
{
 if(carPos+40<180 && carPos+40+speed>=180 && carPos+speed<=220)
{
while(IntersectionBusy)
{
try{ wait();}
catch(InterruptedException e){}
}

         IntersectionBusy=true;
}
if(carPos+speed>220)
{
      IntersectionBusy=false;
}
notify();     //线程退出等待状态
  }
}

1)红色代码部分应该是控制车辆,我不明白为什么这样能分别对车辆进行控制,carPos+40<180 && carPos+40+speed>=180 这个应该是判断到了路口,但是我怎么知道另一辆车是不是刚好经过,carPos+speed<=220,这个也不懂为什么要这样写
2)如果我要两辆车在路口相遇的时候都停下来,该怎么改代码,即故意造成死锁状态(想了解一下)
麻烦各位了,问题比较多

解决方案 »

  1.   

    好像我删了if(carPos+40 <180 && carPos+40+speed>=180 && carPos+speed <=220) 中的carPos+speed <=220条件对结果没影响
      

  2.   

    你可以调试看一下,如果单走横车或者竖车的话,永远都不会进入wait()状态的,因为
    1。横车的情况,carPos=130的时候IntersectionBusy置为true,但是到140的时候不满足条件,所以不会进入wait(),直到重新把IntersectionBusy置为false
    2。竖车的情况,carPos=136的时候IntersectionBusy置为true,但是到153的时候不满足条件,所以不会进入wait(),直到重新把IntersectionBusy置为false但是两辆车一起走的时候,当横车carPos=130并且竖车carPos=136的时候,因为只有一个线程可以取得TrafficCop对象的锁,其中一个线程把IntersectionBusy置为true,然后释放锁,这时候另一个线程获得锁,由于IntersectionBusy=true,
    所以,该线程进入wait()状态,释放掉锁
    (尽管另一个线程执行的时候notify()了,但是由于wait()处于while(IntersectionBusy)的循环中,所以,又继续wait())
    ,直到另一个线程把IntersectionBusy置为false,这时原来"一直wait()"的线程跳出while(IntersectionBusy)的循环得以继续执行。
    这个过程就是这样的,不知道你能明白吗
    至于第2个问题很简单,把wait()注视掉就可以了
      

  3.   

    太感谢您了!!!解释的非常详细耐心,真的非常感谢,对线程的知识不是很了解,请问一下当其中一个线程把IntersectionBusy置为true时,另一个线程运行的时候IntersectionBusy也是为true吗,我还以为是各自独立的
      

  4.   

    为true,但是不满足if的条件,所以不进入while(IntersectionBusy)循环
      

  5.   

    再麻烦一下,对锁的概念不是很清楚,因为只有一个线程可以取得TrafficCop对象的锁,其中一个线程把IntersectionBusy置为true,然后释放锁,这时候另一个线程获得锁,由于IntersectionBusy=true, 
    所以,该线程进入wait()状态,释放掉锁 

    只有一个线程可以获得锁是什么意思,是获得锁才能执行吗,为什么其中一个线程把IntersectionBusy置为true时,就可以释放锁,执行完了?
      

  6.   

    再麻烦一下,对锁的概念不是很清楚,因为只有一个线程可以取得TrafficCop对象的锁,其中一个线程把IntersectionBusy置为true,然后释放锁,这时候另一个线程获得锁,由于IntersectionBusy=true, 
    所以,该线程进入wait()状态,释放掉锁 

    只有一个线程可以获得锁是什么意思,是获得锁才能执行吗,为什么其中一个线程把IntersectionBusy置为true时,就可以释放锁,执行完了?
      

  7.   

    横车取得TrafficCop对象的锁,这个时候,TrafficCop对象checkAndGo方法只有横车可以执行(因为有synchronized关键字),竖车的checkAndGo()方法在等待横车释放锁,(这是个比方,实际执行时,谁先谁后不好说)
    比如carPos=130的时候,横车执行checkAndGo,由于符合if条件,但是IntersectionBusy为false,所以不能执行wait();,然后程序走到IntersectionBusy=true,然后notify();然后函数执行完毕,等到下一次执行的时候,carPos=140,这个时候就不符合if的条件了,所以直接notify();然后函数执行完毕
      

  8.   

    但是当竖车获得锁并在carPos=136的时候,由于符合条件,并且IntersectionBusy为true,所以就wait();因为checkAndGo是synchronized的,所以在线程执行这个方法时候,其他线程不能执行也就是不能获得锁,当方法执行完毕时,就释放锁
      

  9.   

    建议你好好看看,wait(),notify(),notifyAll(),等相关的线程方面的资料,
    可以给你推荐一个资源
    http://www.ibm.com/developerworks/cn/java/j-concurrent/