Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果我没有从控制台输入任何内容,我的程序不会停止_Java_Multithreading - Fatal编程技术网

Java 如果我没有从控制台输入任何内容,我的程序不会停止

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

我有两条线。第一个提问,第二个检查答案。如果我在10个问题之后没有从控制台输入任何内容,那么我的程序不会停止。如何修复它

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();
        }