gpt4 book ai didi

java - 无限检查第二个线程中的条件

转载 作者:行者123 更新时间:2023-12-01 10:35:38 25 4
gpt4 key购买 nike

我正在创建一个基于文本的贪吃蛇游戏,需要无限地检查第二个线程中的条件是否为真,例如:

while(true)
{
if(Snake.moveY == 1)
{
//Do something
}
if(Snake.moveY == -1)
{
//Do something
}
if(Snake.moveX == 1)
{
//Do something
}
if(Snake.moveX == -1)
{
//Do something
}
}

然而,这会导致 StackOverflow 异常,并且永远不会真正运行。我想知道如何不断检查这一点而不引发异常。我确实意识到有人问过这个问题Here ,然而那里的答案都不够充分,并导致 StackOverflow 异常。谢谢!

最佳答案

您需要使用两个线程并共享一个信号。我写了一个演示:

import java.util.Scanner;

/**
* @author heyunxia (love3400wind@163.com)
* @version 1.0
* @since 2016-01-13 下午2:48
*/

public class MainGame {
final MyWaitNotify myWaitNotify = new MyWaitNotify();

public static void main(String[] args) {

final MainGame game = new MainGame();
game.go();
/**/


}

public void go() {

IWorkingThread workingThread = new IWorkingThread() {
@Override
public void execute() {
if ("top".equals(myWaitNotify.getStep())) {
//todo top
System.out.println("moving top...");
} else if ("bottom".equals(myWaitNotify.getStep())) {
//todo bottom
System.out.println("moving bottom...");
} else if ("left".equals(myWaitNotify.getStep())) {
//todo left
System.out.println("moving left...");
} else if ("right".equals(myWaitNotify.getStep())) {
//todo right
System.out.println("moving right...");
}
}
};
ControlThread controlThread = new ControlThread(myWaitNotify, workingThread);
controlThread.start();



new Thread(new Runnable() {
@Override
public void run() {
while (true) {
Scanner cin=new Scanner(System.in);
System.out.println("please input:");
String name=cin.nextLine();
signal(name);
}
}
}).start();
}

private void signal(String step){
myWaitNotify.setStep(step);
//very important
myWaitNotify.doNotify();
}


interface IWorkingThread {
void execute();
}


class MonitorObject {
}

class MyWaitNotify {
MonitorObject myMonitorObject = new MonitorObject();
boolean wasSignalled = false;
private String step = "sleep"; // default single : sleep

public String getStep() {
return step;
}

public void setStep(String step) {
this.step = step;
}

public void doWait() {
synchronized (myMonitorObject) {
while (!wasSignalled) {
try {
myMonitorObject.wait();
} catch (InterruptedException e) {
}
}
wasSignalled = false;
}
}

public void doNotify() {
synchronized (myMonitorObject) {
wasSignalled = true;
myMonitorObject.notify();
}
}
}


class ControlThread extends Thread {
private MyWaitNotify waitNotify;
private IWorkingThread workingThread;

public ControlThread(MyWaitNotify waitNotify, IWorkingThread workingThread) {
this.waitNotify = waitNotify;
this.workingThread = workingThread;
}

public void run() {
while (true) {
//System.out.println();
System.out.println(waitNotify.getStep());
waitNotify.doWait();
if ("#left#right#top#bottom".contains(waitNotify.getStep())) {
this.workingThread.execute();
} else /* exit */ {//game over
//todo exit code
}
}
}
}
}

关于java - 无限检查第二个线程中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34757632/

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