문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

The output is being buffered.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top