这段代码可运行,是关于模拟车辆通过交通路口的程序,下面这段程序已经实现对车辆进行控制,不会发生碰撞,我看不太懂,请高手帮忙解答一下
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)如果我要两辆车在路口相遇的时候都停下来,该怎么改代码,即故意造成死锁状态(想了解一下)
麻烦各位了,问题比较多
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)如果我要两辆车在路口相遇的时候都停下来,该怎么改代码,即故意造成死锁状态(想了解一下)
麻烦各位了,问题比较多
解决方案 »
- 等待窗口在屏幕上出现时,在下边任务栏不显示相应窗口条目,有办法吗?
- 关于加密还原问题,急待!!!
- 高分求解!100分
- java是怎样从底层完整的实现Hash表的
- 100分,请问现在公司收违约金的现象普遍吗?如果收的话,大概是多少呢?
- 如何在Jbuilder下连接SQL Server2000 求教,急
- 请问怎么把一棵jtree树写为XML文件?
- String.valueOf(Object obj)返回一个String,这个String的内容代表了obj的什么?
- java查询数据库的问题:如果得到的结果是多条record的时候怎么保存?
- java程序是如何进入消息循环等待的
- 问一个对你们很easy的问题!在线等
- 线程休眠的问题
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()注视掉就可以了
所以,该线程进入wait()状态,释放掉锁
只有一个线程可以获得锁是什么意思,是获得锁才能执行吗,为什么其中一个线程把IntersectionBusy置为true时,就可以释放锁,执行完了?
所以,该线程进入wait()状态,释放掉锁
只有一个线程可以获得锁是什么意思,是获得锁才能执行吗,为什么其中一个线程把IntersectionBusy置为true时,就可以释放锁,执行完了?
比如carPos=130的时候,横车执行checkAndGo,由于符合if条件,但是IntersectionBusy为false,所以不能执行wait();,然后程序走到IntersectionBusy=true,然后notify();然后函数执行完毕,等到下一次执行的时候,carPos=140,这个时候就不符合if的条件了,所以直接notify();然后函数执行完毕
可以给你推荐一个资源
http://www.ibm.com/developerworks/cn/java/j-concurrent/