我不知道如何实现这一点。我想要一个计时器,它可以永远保持循环,但是当该循环运行时,我希望能够让扫描仪运行并等待用户输入。基本上就像聊天一样。循环会不断检查是否有新发布的消息,但在循环运行时用户仍然可以提交消息。
我该怎么做?
另外我还没有 GUI 设置。这一切都在一个简单的命令提示符下运行(如果重要的话)。
I'm not sure how to accomplish this. I want to have a timer which would keep a loop on forever but while that loop is running I want to be able to have a Scanner running and waiting for user input. Basically like a chat. Where the loop would constantly check for any new messages posted but while the loop is running the user can still submit messages.
How can I do this?
Also I dont have a GUI setup yet. This is all running in a simple command prompt (if it matters).
发布评论
评论(4)
您可以使用工作线程设计模式来设计它。请参阅以下链接以获取更多信息:
You can use Worker Thread design pattern to design this. Please refer following links to get more idea on this:
您可以使用多线程应用程序,但在我看来,这比您想要的要复杂一些。
:
尝试只定义一个计时器,并向其中添加一个计时器任务,然后在循环中接收输入:
You could use a multiple thread application, however it seems to me that would be a bit more complicated than what your looking for.
:
Try just defining a timer, and adding a timertask to it, and then taking in input in a loop:
您可以运行一个正在等待输入的线程。输入输入后,主程序可以调用线程中的方法,然后该方法可以决定是终止循环还是继续循环。
You can have a thread running which is waiting for the input. Once the input is entered, the main program can call a method in the thread which can then decide whether to terminate the loop or keep going.
嗯,听起来还很早期,但是您可以执行多个线程,或者您可以让循环在来自用户的新消息之间来回跳动,并检查是否有新内容发布。
如果您正在从
InputStream
(或其子类之一)读取数据,则可以调用available
方法来检查可用字节数是否> >。 0(如果有任何内容需要从其他聊天参与者或用户输入中读取)。因此,一个循环:
这基本上就是它的全部内容。
Well, sounds pretty early stage, but you could either do multiple Threads, or you could just have your loop bounce back and forth between new messages from the user, and checking to see if there are new things posted.
If you're reading from an
InputStream
(or one of its sub-classes) you can call theavailable
method to check and see if the number of available bytes if > 0 (if there is anything to be read from either the other chat participants, or the user input).So, a loop:
That's basically all there is to it.