Java 如果我没有从控制台输入任何内容,我的程序不会停止
我有两条线。第一个提问,第二个检查答案。如果我在10个问题之后没有从控制台输入任何内容,那么我的程序不会停止。如何修复它Java 如果我没有从控制台输入任何内容,我的程序不会停止,java,multithreading,Java,Multithreading,我有两条线。第一个提问,第二个检查答案。如果我在10个问题之后没有从控制台输入任何内容,那么我的程序不会停止。如何修复它 public void produce() throws InterruptedException { for (this.i = 0; this.i < 10; this.i++) { if (this.exit) { try { this.lock.lo
public void produce() throws InterruptedException {
for (this.i = 0; this.i < 10; this.i++) {
if (this.exit) {
try {
this.lock.lock();
Bot.LOGGER.info(this.questions.get(this.i));
} finally {
this.lock.unlock();
Thread.sleep(5000);
}
}
}
}
您是否设置过
this.exit
?如何初始化this.i
?此循环是否已退出?这是代码的一部分。this.exit和this.i已初始化如何声明this.exit
?它应该声明为volatile
,并且看起来您的程序应该在第一个正确答案后退出
public void consume() throws InterruptedException {
final Scanner scanner = new Scanner(System.in);
String inputString = "";
while (this.exit) {
try {
if ((!(inputString = scanner.nextLine()).equals(""))) {
this.lock.lock();
if (inputString.equals(this.answers.get(this.i))) {
Bot.LOGGER.info("Correct!");
Bot.LOGGER.info("The program was finished");
this.exit = false;
}
else {
Bot.LOGGER.info("Wrong answer!");
}
}
} finally {
this.lock.unlock();
Thread.sleep(500);
}
}
scanner.close();
}