我正在用 Java 构建一个控制台游戏,其工作原理如下:它会打印一个操作(例如:3 x 4),并且您必须写入结果(在本例中为 12),并且它将在1分钟后就会结束。
我从一开始就知道我必须使用线程来捕获用户输入,所以这就是线程的逻辑:
public class UserInput extends Thread {
private int answer;
@Override
public void run() {
Scanner in = new Scanner(System.in);
while(true){
answer = in.nextInt();
}
}
public int getAnswer(){
return answer;
}
}
很简单,现在游戏的逻辑:
public static void play() {
Game game = new EasyGame();
UserInput ui = new UserInput();
long beginTime = System.currentTimeMillis()/1000;
ui.start();
boolean accepted = true;
while(timeLeft(beginTime)){
//PrintChallenge() prints an operation and store its result in game
if(accepted) game.PrintChallenge();
accepted = false;
if(ui.getAnswer() == game.getResult()) accepted = true;
}
}
//returns if current time is 60 seconds after initial time
public static boolean timeLeft(long time){
return (System.currentTimeMillis()/1000) < (time + 60);
}
但它不起作用,它根本不会将 ui 的 getAnswer()
与游戏的 getResult()
匹配。我在这个线程和游戏逻辑上做错了什么?
最佳答案
我认为你的问题是Java在本地缓存你的int值,尽管这可能是由于你的game.getResult()中的某些内容造成的,因为我无法检查这一点。 java中线程安全很难。
确认:
- 我构建了游戏的愚蠢版本,没有任何游戏逻辑或计时器。
- 我在您的答案 int 中添加了一个 volatile 关键字,这使得 Java 检查主内存而不是本地缓存来获取 int 的值。
一旦用户输入“30”,就会输出以下代码,删除用户输入中的“ volatile ”关键字会导致您遇到的情况。
见下文:
package stackOverflowTests;
import java.util.Scanner;
public class simpleGame {
public static class UserInput extends Thread {
volatile private int answer;
public void run() {
Scanner in = new Scanner(System.in);
while(true){
System.out.print("Answer meeee!:");
answer = in.nextInt();
}
}
public int getAnswer(){
return answer;
}
}
public static void play() throws InterruptedException {
UserInput testInput = new UserInput();
testInput.start();
while(true){
//PrintChallenge() prints an operation and store its result on game
Thread.sleep(10);
if(testInput.getAnswer()==30)System.out.println(testInput.getAnswer()+ " : "+(testInput.getAnswer()==10));
}
}
public static void main(String[] args) throws InterruptedException{
play();
}
}
关于Java访问Thread的类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44559674/