如何让扫描仪运行一段时间才能获得输入?

发布于 2024-10-27 08:12:09 字数 436 浏览 4 评论 0原文

我正在尝试编写一个循环,该循环运行直到我在运行应用程序的控制台中键入特定文本。比如:

while (true) {
try {
    System.out.println("Waiting for input...");
    Thread.currentThread();
    Thread.sleep(2000);
    if (input_is_equal_to_STOP){ // if user type STOP in terminal
        break;
    }
} catch (InterruptedException ie) {
    // If this thread was intrrupted by nother thread
}}

我希望它每次通过时都写一行,所以我不希望它在一段时间内停止并等待下一个输入。我需要为此使用多个线程吗?

I'm trying to write a loop which runs until I type a specific text in console where the application is running. Something like:

while (true) {
try {
    System.out.println("Waiting for input...");
    Thread.currentThread();
    Thread.sleep(2000);
    if (input_is_equal_to_STOP){ // if user type STOP in terminal
        break;
    }
} catch (InterruptedException ie) {
    // If this thread was intrrupted by nother thread
}}

And I want it to write a line each time it pass through so I do not want it to stop within the while and wait for next input. Do I need to use multiple threads for this?

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

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

发布评论

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

评论(2

几度春秋 2024-11-03 08:12:09

我需要为此使用多个线程吗?

是的。

由于在 System.in 上使用 Scanner 意味着您正在执行阻塞 IO,因此需要一个线程专门用于读取用户输入的任务。

这是一个帮助您入门的基本示例(不过,我鼓励您查看 java.util.concurrent 包来执行这些类型的操作。):

import java.util.Scanner;

class Test implements Runnable {

    volatile boolean keepRunning = true;

    public void run() {
        System.out.println("Starting to loop.");
        while (keepRunning) {
            System.out.println("Running loop...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        System.out.println("Done looping.");
    }

    public static void main(String[] args) {

        Test test = new Test();
        Thread t = new Thread(test);
        t.start();

        Scanner s = new Scanner(System.in);
        while (!s.next().equals("stop"));

        test.keepRunning = false;
        t.interrupt();  // cancel current sleep.
    }
}

Do I need to use multiple threads for this?

Yes.

Since using a Scanner on System.in implies that you're doing blocking IO, one thread will need to be dedicated for the task of reading user input.

Here's a basic example to get you started (I encourage you to look into the java.util.concurrent package for doing these type of things though.):

import java.util.Scanner;

class Test implements Runnable {

    volatile boolean keepRunning = true;

    public void run() {
        System.out.println("Starting to loop.");
        while (keepRunning) {
            System.out.println("Running loop...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        System.out.println("Done looping.");
    }

    public static void main(String[] args) {

        Test test = new Test();
        Thread t = new Thread(test);
        t.start();

        Scanner s = new Scanner(System.in);
        while (!s.next().equals("stop"));

        test.keepRunning = false;
        t.interrupt();  // cancel current sleep.
    }
}
荆棘i 2024-11-03 08:12:09

是的,为此您需要两个线程。第一个可以做这样的事情:

//accessible from both threads
CountDownLatch latch = new CountDownLatch(1);

//...
while ( true ) {
    System.out.println("Waiting for input...");
    if ( latch.await(2, TimeUnit.SECONDS) ) {
       break;
    }
}

另一个:

Scanner scanner = new Scanner(System.in);
while ( !"STOP".equalsIgnoreCase(scanner.nextLine()) ) {
}
scanner.close();
latch.countDown();

Yes, you would need two threads for this. The first could do something like this:

//accessible from both threads
CountDownLatch latch = new CountDownLatch(1);

//...
while ( true ) {
    System.out.println("Waiting for input...");
    if ( latch.await(2, TimeUnit.SECONDS) ) {
       break;
    }
}

And the other:

Scanner scanner = new Scanner(System.in);
while ( !"STOP".equalsIgnoreCase(scanner.nextLine()) ) {
}
scanner.close();
latch.countDown();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文