Domanda

I'm trying to change the output in Windows console dynamically with this code:

for(int i = 0;; i++,Thread.sleep(dur!("msecs")(200)))
    write("\r",i);

But you'll have to wait for a long time to see something on screen. Even changed to 1 msec, the duration will be enough to slow down numbers rewriting to 2-3 seconds.

The output changes like this (somewhere about):

23343
30948332
42048332

It seems like the write() function work can be interrupted at the middle of process.

If we change write() to writeln(), sleep() function will work correctly.

Why does it happening so?

È stato utile?

Soluzione

Use stdout.flush() after every call to write. It's because writing is buffered by default, whereas writeln (unlike write) flushes the output by default.

Altri suggerimenti

The output is being buffered.

Insert a call to stdout.flush() before the Thread.sleep calls to see the output immediately.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top