1
1
123qwe2015-11-14 14:55:02
Java
123qwe, 2015-11-14 14:55:02

What is the best way to update information in a stream?

What is up, programmer.
There is a thread that runs until a certain action occurs.
All it does is display a message.
I need to make it so that every second it displays 1 of 3 different messages in order.
For example:
I'm waiting.
Then it's "I'm waiting." disappears, and in its place appears "Waiting ..",
then "Waiting ..." and all over again. Just so that only the points are updated.
How to implement this? I tried to call all sorts of update methods, it doesn’t work) Help.
ps: This is most likely a poor implementation of what I want to do, but I'm interested in trying it this way)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vlad20012, 2015-11-15
@vlad20012

public class Waiter {
    private static volatile boolean isWaiting;

    public static void start() {
        if(isWaiting) throw new IllegalStateException();
        isWaiting = true;
        System.out.print("Жду .");
        new Thread(() -> {
            for(int i = 1; isWaiting; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ignored) {}
                System.out.print(i % 3 == 0 ? "\b\b" : ".");
            }
            System.out.println();
        }).start();
    }

    public static void stop() {
        if(!isWaiting) throw new IllegalStateException();
        isWaiting = false;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question