gpt4 book ai didi

Java - 这个错过的信号解决方案好吗?

转载 作者:行者123 更新时间:2023-11-30 10:57:44 24 4
gpt4 key购买 nike

我创建了一个游戏,我希望玩家在离开一段时间后被移除。可运行的 PlayerRemover 类包含可运行的 GameTimer 类的实例。 PlayerRemover 创建一个 GameTimer 线程,该线程超时或手动停止,之后它通知 PlayerRemover 线程继续。

我担心如果在 wait() 之前调用 notify() 可能会错过信号,所以我决定让 GameTimer 线程发出通知,直到 PlayerRemover 线程将 GameTimer 中的 boolean 变量设置为 false。

我在网上寻找了几个解决信号丢失的解决方案,但没有提到这个,并且使用带有原子代码块的 while 循环让我想知道是否有充分的理由这样做。

我的代码运行良好,但这种方法会出现问题吗?有没有更好更标准的方法来做到这一点?

感谢您的帮助,谢谢!

public class PlayerRemover implements Runnable
{
private final GameTimer timer;
private final int seat;
private final BlackjackPlayer p;
private boolean wasSignalled;

public PlayerRemover(TableFrame gui, GameTimer t)
{
timer = t;
seat = gui.getTablePanel().getSeatIndex() ;
p = gui.getTablePanel().getBlackjackPlayer();
wasSignalled = false;
}

@Override
public void run()
{
Thread timerThread = new Thread(timer);
timerThread.start();

synchronized(timerThread)
{
while (g[seat] != null && p.getState() == State.SITTING_OUT && timer.getSecondsLeft() > 0)
{
try {
timerThread.wait();
} catch (InterruptedException ex) {
Logger.getLogger(TableCoord.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

timer.setSignalRecieved();
timer.stopTimer();

if (g[seat] != null && timer.getSecondsLeft() == 0)
{
removePlayer(p,seat);
updateAllGUIs();
}
}
}



public class GameTimer implements Runnable {

private int secondsLeft;
private boolean timerStop;
private boolean doNotify;
private boolean signalReceived;

/**
* Creates a timer with a given number of seconds on the clock.
*/
public GameTimer(int seconds,boolean notifyThis)
{
secondsLeft = seconds;
timerStop = false;
doNotify = notifyThis;
signalReceived = false;
}

public GameTimer(int seconds)
{
secondsLeft = seconds;
timerStop = false;
doNotify = false;
}

/**
* Stops timer permanently
*/
public void stopTimer()
{
timerStop = true;
}

public int getSecondsLeft()
{
return secondsLeft;
}

public boolean getTimerStop()
{
return timerStop;
}

public void setSignalRecieved()
{
signalReceived = true;
}



@Override
public void run()
{
// While there timer is still counting down or all players finish
// their actions.
while (!timerStop)
{
// Wait 1 second
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
System.out.println("Error: " + e.toString());
}

//decrement timer 1 second
secondsLeft--;

if (secondsLeft <= 0)
{
timerStop = true;
}
}

timerStop= true;
if (doNotify)
{
while (!signalReceived)
{
synchronized(this)
{
notify();
try
{
Thread.sleep(100);
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
}
}
}
}
}
}

最佳答案

对于大多数任务,wait() 和 notify() 方法通常太低级且容易出错。 ScheduledExecutorService 是一种简单且高级的任务调度方法。

JavaDoc 中的一个示例演示了固定速率可重复任务和一次性任务:

Here is a class with a method that sets up a ScheduledExecutorService to beep every ten seconds for an hour:

import static java.util.concurrent.TimeUnit.*;


class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);

public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}

关于Java - 这个错过的信号解决方案好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32486537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com