Java - 循环同时保持另一个代码运行

发布于 2024-12-10 09:51:50 字数 177 浏览 0 评论 0 原文

我不知道如何实现这一点。我想要一个计时器,它可以永远保持循环,但是当该循环运行时,我希望能够让扫描仪运行并等待用户输入。基本上就像聊天一样。循环会不断检查是否有新发布的消息,但在循环运行时用户仍然可以提交消息。

我该怎么做?

另外我还没有 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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瞳孔里扚悲伤 2024-12-17 09:51:50

您可以使用多线程应用程序,但在我看来,这比您想要的要复杂一些。

:
尝试只定义一个计时器,并向其中添加一个计时器任务,然后在循环中接收输入:

    Scanner scan = new Scanner(System.in);
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            //Do whatever you want with your messages here
        }
    }, 0, 1000);

    //Wait for user input
    while(true) {
        String bar = scan.next();
    }

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:

    Scanner scan = new Scanner(System.in);
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            //Do whatever you want with your messages here
        }
    }, 0, 1000);

    //Wait for user input
    while(true) {
        String bar = scan.next();
    }
风筝在阴天搁浅。 2024-12-17 09:51:50

您可以运行一个正在等待输入的线程。输入输入后,主程序可以调用线程中的方法,然后该方法可以决定是终止循环还是继续循环。

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.

烟雨凡馨 2024-12-17 09:51:50

嗯,听起来还很早期,但是您可以执行多个线程,或者您可以让循环在来自用户的新消息之间来回跳动,并检查是否有新内容发布。

如果您正在从InputStream(或其子类之一)读取数据,则可以调用available方法来检查可用字节数是否> >。 0(如果有任何内容需要从其他聊天参与者或用户输入中读取)。

因此,一个循环:

  • if (newMessages) ...发布新消息...
  • if (newUserChat) ...发送新消息...

这基本上就是它的全部内容。

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 the available 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:

  • if (newMessages) ...post the new messages...
  • if (newUserChat) ...send new message out...

That's basically all there is to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文