简体   繁体   English

Java输入无需暂停

[英]Java input without pausing

How would I make a sort of console for my program without pausing my code? 我如何在不暂停代码的情况下为程序制作一个控制台? I have a loop, for example, that needs to stay running, but when I enter a command in the console, I want the game to check what that command was and process it in the loop. 例如,我有一个循环需要保持运行,但是当我在控制台中输入命令时,我希望游戏检查该命令是什么并在循环中对其进行处理。 The loop shouldn't wait for a command but just have an if statement to check if there's a command in the queue. 循环不应该等待命令,而应该使用if语句检查队列中是否有命令。

I'm making a dedicated server, by the way, if that helps. 顺便说一句,如果有帮助,我正在制造一台专用服务器。

There's pretty obvious approach: use a dedicated thread to wait on InputStream, read events/commands from it and pass them into a queue. 有一个非常明显的方法:使用专用线程等待InputStream,从中读取事件/命令,然后将它们传递到队列中。

And your main thread will be regularly checking this queue. 您的主线程将定期检查此队列。 After every check it will either process a command from the queue or continue what it was doing if it's empty. 每次检查后,它都将处理队列中的命令,或者如果它为空,则继续执行该操作。

What you'd like to have is a thread in which you keep the command reading code running. 您想要的是一个线程,您可以在其中保持命令的运行状态。 It'd probably look something like this: 它可能看起来像这样:

class ReadCommand implements Runnable
{
    public void run()
    {
       // Command reading logic goes here
    }
}

In your "main" thread where the rest of the code is running, you'll have to start it like this: 在运行其余代码的“主”线程中,您必须像这样启动它:

new Thread(new ReadCommand())).start()

Additionally, you need a queue of commands somewhere which is filled from ReadCommand and read from the other code. 另外,您需要在某处放置一个命令队列,该队列由ReadCommand填充并从其他代码读取。 I recommend you to read a manual on concurrent java programming. 我建议您阅读有关并发Java编程的手册。

Have them run in two separate threads. 让它们在两个单独的线程中运行。

class Server {

    public static void main(String[] args) {
        InputThread background = new InputThread(this).start();
        // Run your server here
    }
}

class InputThread {
    private final Server server;
    public InputThread(Server server) {
        this.server = server;
    }

    public void run() {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()) {
            // blocks for input, but won't block the server's thread
        }
    }
}

The server should run in its own thread. 服务器应在其自己的线程中运行。 This will allow the loop to run without pausing. 这将允许循环运行而不会暂停。 You can use a queue to pass commands into the server. 您可以使用队列将命令传递到服务器。 Each time through the loop the server can check the queue and process one or more commands. 每次通过循环,服务器都可以检查队列并处理一个或多个命令。 The command line can then submit commands into the queue according to its own schedule. 然后,命令行可以根据自己的时间表将命令提交到队列中。

You can read from the console in a separate thread. 您可以在控制台中的单独线程中进行读取。 This means your main thread doesn't have to wait for the console. 这意味着您的主线程不必等待控制台。

Even server applications can have a Swing GUI. 甚至服务器应用程序也可以具有Swing GUI。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM