我正在开发机制来处理游戏的武器。我遇到了射速问题,它适用于单线程,但似乎我的 GUI 正在从多个线程调用 fire() 方法,这有时会导致一些意外行为(多发射击非常快)。所以我在方法中添加了一个锁(带有一个AtomicBoolean
标志),但它只会导致错误出现的频率降低。
武器类
private final AtomicBoolean locked = new AtomicBoolean(false);
private final AtomicInteger fired = new AtomicInteger(0);
@Override
// returns true if a shot was fired; false otherwise
public boolean fire() {
if (locked.compareAndSet(false, true)) {
return false;
}
if (!canFire || reloading)
return false;
if (bulletsInMagazine > 0) {
canFire = false;
timeSinceLastShot = 0;
bulletsInMagazine--;
fired.incrementAndGet();
if (bulletsInMagazine == 0) {
reload();
}
locked.set(false);
return true;
}
reload();
locked.set(false);
return false;
}
@Override
public void tick(int ms) {
timeSinceLastShot += ms;
if (timeSinceLastShot >= delayBetweenShotsMs) {
canFire = true;
}
remainingReloadTime = Math.max(0, remainingReloadTime - ms);
if (remainingReloadTime == 0 && reloading)
processReloading();
}
我写了一个单元测试来重现问题......有时。
测试:
@Test
public void testFiringRate() {
int nbBullets = weapon.getBulletsInMagazine();
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(16);
for (int i = 0; i < 16; i++) {
scheduledExecutorService.scheduleAtFixedRate(weapon::fire, 5, 10, TimeUnit.MILLISECONDS);
}
System.out.println("START firing");
int ticks = 0;
while (ticks < 100) {
weapon.tick(100);
ticks++;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
int fired = nbBullets - weapon.getBulletsInMagazine();
int reallyFired = weapon.getFiredShotCount();
int diff = reallyFired - fired;
if (diff != 0) {
System.out.println(ticks + "\t" + fired + " shot(s)\t" + reallyFired + " really fired\t" + (diff != 0 ? "(+" + diff + ")" : ""));
} else {
System.out.print(".");
}
}
System.out.println("\nSTOP firing");
System.out.println(ticks + " Ticks");
try {
System.out.print("WAITING FOR ALL TASKS TO TERMINATE...");
scheduledExecutorService.shutdown();
scheduledExecutorService.awaitTermination(5, TimeUnit.SECONDS);
System.out.println("DONE.");
} catch (InterruptedException e) {
e.printStackTrace();
}
int bulletsAfter = weapon.getBulletsInMagazine();
int fired = nbBullets - bulletsAfter;
System.out.println("Shot fired: " + fired);
int shotsFired = weapon.getFiredShotCount();
System.out.println("Really fired: " + shotsFired);
assertEquals(shotsFired, fired);
}
我得到的(有时):计算出的AtomicInteger
枪
数比实际少 1 或 2
我想要 的是:无论线程数如何,武器都应该尊重它的射速。
任何想法 ?
编辑:
我试图反转 if 条件:
if (!locked.compareAndSet(false, true)) {
return false;
}
它从单元测试中解决了问题,但现在 GUI 只触发一次,再也不会触发。