Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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_Interrupt - Fatal编程技术网

Java 计数器应用程序没有';停不下来

Java 计数器应用程序没有';停不下来,java,multithreading,interrupt,Java,Multithreading,Interrupt,我制作了一个计数器应用程序,当用户在控制台中输入“stop”时,它使用线程中断计数。我仔细检查了我的代码,没有发现问题。我是线程新手,所以任何人都可以看看这个 import java.util.Scanner; public class CounterInterruptApp { public static void main(String[] args) { new CounterInterruptApp().start(); } publ

我制作了一个计数器应用程序,当用户在控制台中输入“stop”时,它使用线程中断计数。我仔细检查了我的代码,没有发现问题。我是线程新手,所以任何人都可以看看这个

import java.util.Scanner;

public class CounterInterruptApp 
{

    public static void main(String[] args) 
    {
      new CounterInterruptApp().start();
    }

    public void start()
    {
        Thread counter = new Counter(); //Instantiate the counter thread.
        counter.start(); //Start the counter thread.

        Scanner scanner = new Scanner(System.in);
        String s = "";
        while(!s.equals("stop")); //Wait for the user to enter stop.
        s=scanner.next();
        counter.interrupt(); //Interrupt the counter thread.
    }

}



public class Counter extends Thread //Extend Thread for the use of the Thread Interface.
{
    public void run()//Run method.  This is part of the Thread interface.
    {
        int count = 0;
        while(!isInterrupted())
        {
            System.out.println(this.getName() + "Count: " + count);
            count++;
            try //Try/Catch statement.
            {
              Thread.sleep(1000); //Make the Thread sleep for one second.
            } catch(InterruptedException e) {
              break;
            }
        }
        System.out.println("Counter Interrupted."); //Display the message Counter Interrupted.
    }

}

您的while循环检查“stop”字符串的格式错误。 应该是这样的:

while(!s.equals("stop"))
{   //Wait for the user to enter stop.
    s=scanner.nextLine();
}
counter.interrupt();  //Interrupt the counter thread.

您的while循环检查“stop”字符串的格式错误。 应该是这样的:

while(!s.equals("stop"))
{   //Wait for the user to enter stop.
    s=scanner.nextLine();
}
counter.interrupt();  //Interrupt the counter thread.

您的控制台输出是什么?你收到“计数器中断”信息了吗?没有,这是我的输出。请注意,我已尝试按照while(!s.equals(“stop”)的指定停止程序线程计数:0线程计数:0线程计数:1线程计数:2线程计数:3停止线程计数:4线程计数:5停止线程计数:6停止线程计数:7停止线程计数:8停止线程计数:9线程计数:10线程计数:11线程计数:12线程计数:13线程计数:14线程计数:15控制台输出是什么?你得到答案了吗“计数器中断”消息?不,这是我的输出。请注意,我已尝试按照while(!s.equals(“stop”)指定的方式停止程序;线程计数:0线程计数:1线程计数:2线程计数:3停止线程计数:4线程计数:5停止线程计数:6停止线程计数:7停止线程计数:8停止线程计数:9线程计数:10线程计数:11线程计数:12线程计数:13线程计数:14线程计数sole无法中断程序。@ConorPendlebury应在运行时删除分号。键入“hello”后是否按enter键?也请注意字母大小写。即使使用花括号,控制台中的停止也无法中断程序。@ConorPendlebury应在运行时删除分号。..a在输入“hello”后是否按enter键?请注意字母大小写。。