项目场景:
前段时间跟着B站视频完成(fuke)了一个打飞机的小游戏,基本上跟着做的。项目使用了多线程,分成五个对象实现游戏功能。
GameFrame: 游戏框架,main程序
HeroPlane:玩家飞机
EnemyPlane:敌机
Bullet:子弹
Player:玩家操作
问题描述
完成后自己玩了几轮,发现偶尔会冒出来一个数组下标越界异常,但是不影响游戏进行,但是就没管了。隔了一段时间想起来,决定解决这个BUG。
BUG如下:
Exception in thread “Thread-31” java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
at java.base/java.util.Vector.get(Vector.java:750)
at domain.EnemyPlane.hit(EnemyPlane.java:58)
at domain.EnemyPlane.run(EnemyPlane.java:31)
原因分析:
来到EnemyPlane类的第58行,看到是bullets集合获取元素时候下标越界了(Bullet bullet = gf.bullets.get(i);)。
public boolean hit(){
Rectangle rec_ep = new Rectangle(this.x, this.y, this.width, this.height);
Rectangle rec_bullet =null;
for (int i = 0; i < gf.bullets.size(); i++) {
Bullet bullet = gf.bullets.get(i);
rec_bullet = new Rectangle(bullet.x,bullet.y,bullet.width,bullet.height);
if(rec_ep.intersects(rec_bullet)){
gf.bullets.remove(bullet);
return true;
}
}
return false;
}
这里的bullets被定义为Vector集合的。
Vector<Bullet> bullets =new Vector<>(); //子弹集合
看来,是因为在fori循环中,可能某一个线程正好remove 了 bullets集合最后一个元素(gf.bullets.remove(bullet);),这时候另一个线程进入循环get最后一个元素(gf.bullets.get(i);),因此报错数组下标越界了。
如果将fori循环改成成foreach循环,则直接报ConcurrentModificationException(并发异常)。
这就奇怪了,不是说Vector集合是线程安全的吗?为什么还会出现此错误。
仔细一想,Vector类中对get以及remove,size方法都加了synchronized关键字来保证同步,也就说当一个线程调用了这些方法时,其他线程不能再同时调用这些方法。换言之,不能出现两个及两个以上的线程在同时调用Vector集合的同一个方法,但是可以调用不同的方法呀。
比如这样:
1: Thread-A get(i)最后一个元素,
2: Thread-A 这时候 remove 了 bullets集合最后一个元素,
3: Thread-B get(i)最后一个元素。
于是报错下标越界了。
在这个程序里,ArrayIndexOutOfBoundsException出现的概率其实比较低,有时候我玩好久也没出现。因为只有i== bullets.size()-1 的时候才会出现。
而且,由于remove了一个元素,后面的元素的下标会-1,补充原来的元素位置,当i不等于bullets.size()-1 的时候,虽然程序不会报错,但是在某些情况下会导致Thread-B获取的元素不是原本计划中的那一个,进而导致某些问题。
解决方案:
解决方法其实很简单,加上synchronized(this){……}代码块,包裹fori循环即可。
public boolean hit(){
Rectangle rec_ep = new Rectangle(this.x, this.y, this.width, this.height);
Rectangle rec_bullet =null;
synchronized(this){
for (int i = 0; i < gf.bullets.size(); i++) {
Bullet bullet = gf.bullets.get(i);
rec_bullet = new Rectangle(bullet.x,bullet.y,bullet.width,bullet.height);
if(rec_ep.intersects(rec_bullet)){
gf.bullets.remove(bullet);
return true;
}
}
}
return false;
}