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

Java控制台应用程序:接受来自无限循环的命令

Java控制台应用程序:接受来自无限循环的命令,java,console,Java,Console,因此,我有一个无限循环在控制台应用程序的主线程中运行,就像这样 public static void main(String[] args) { while(true){ doSomething(); doSomethingElse(); System.out.println("some debugging info"); doAnotherThing(); } } 我希望这段代码反复运行 偶尔,我想在控制台中输入

因此,我有一个无限循环在控制台应用程序的主线程中运行,就像这样

public static void main(String[] args) {
    while(true){
        doSomething();
        doSomethingElse();
        System.out.println("some debugging info");
        doAnotherThing();
    }
}
我希望这段代码反复运行

偶尔,我想在控制台中输入一个命令,比如字符串“givemoreinfoplox”,然后如果该命令等于某个值,我希望我的代码做一些事情


通常我只会使用扫描仪,但我不能在这里这样做-因为scanner.next();暂停我的代码。。。无论是否输入命令,我都希望代码保持运行。我能看到的唯一解决方法是使用文件。但是还有其他选择吗?

使用线程,一个主线程从控制台读取,另一个线程执行循环,第一个线程更新字符串列表(生产者),循环线程读取列表以查看是否有新内容(消费者)

使用线程,一个主线程从控制台读取,另一个线程执行循环,第一个线程更新字符串列表(生产者),循环线程读取列表以查看是否有新内容(消费者)

您可以尝试
System.in.available()
,它是非阻塞的。但是,该方法被称为没有很好的规范。在某些系统(基于Unix的OpenJDK)上,它仅在用户使用
enter
键确认输入后返回
>0


否则,morgano建议在一个单独的线程上持续阻塞
System.in.read()

您可以尝试
System.in.available()
,这是非阻塞的。但是,该方法被称为没有很好的规范。在某些系统(基于Unix的OpenJDK)上,它仅在用户使用
enter
键确认输入后返回
>0


否则,morgano建议在一个单独的线程上连续阻塞
System.in.read()

您可以执行以下操作

public class MainApp implements Runnable
{

    static String command;
    static boolean newCommand = false;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        MainApp reader = new MainApp();
        Thread t = new Thread(reader);
        t.start();

        while (true)
        {
            doSomething();

            if (newCommand)
            {
                System.out.println("command: " + command);
                newCommand = false;
                //compare command here and do something
            }
        }
    }

    private static void doSomething()
    {
        try
        {
            System.out.println("going to do some work");
            Thread.sleep(2000);
        } catch (InterruptedException ex)
        {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run()
    {
        Scanner scanner = new Scanner(System.in);

        while(true)
        {
            command = scanner.nextLine();
            System.out.println("Input: " + command);
            newCommand= true;
        }
    }



}

你可以做下面这样的事情

public class MainApp implements Runnable
{

    static String command;
    static boolean newCommand = false;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        MainApp reader = new MainApp();
        Thread t = new Thread(reader);
        t.start();

        while (true)
        {
            doSomething();

            if (newCommand)
            {
                System.out.println("command: " + command);
                newCommand = false;
                //compare command here and do something
            }
        }
    }

    private static void doSomething()
    {
        try
        {
            System.out.println("going to do some work");
            Thread.sleep(2000);
        } catch (InterruptedException ex)
        {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run()
    {
        Scanner scanner = new Scanner(System.in);

        while(true)
        {
            command = scanner.nextLine();
            System.out.println("Input: " + command);
            newCommand= true;
        }
    }



}

提示:你不想无限重复这件事,你只想让它一直等到事情发生。提示:你不想无限重复这件事,你只想让它一直等到事情发生。