简体   繁体   中英

How to pause input when using Thread.sleep in Java

When I pause the execution in my program with Thread.sleep , it seems that the program is still able to get input, even while it is being paused. Is there any fix to this? I am using Java 2 and I've looked through so many forums, but couldn't find a suitable answer.

This is the code I'm using to pause my program:

Thread.sleep(2000);
int input = c.readInt();

Your program does not control the terminal device. You can see what you type because the software that actually controls the device is reading keyboard input and writing it to the screen. Your program is not getting any input while it's in sleep.

What actually controls the terminal window or device depend on operating system, and things get complicated when dive into the details. For example on Unix, it's handled by aa thing called "line discipline". The default ("canonical") setting lets you edit lines of text before they are sent to your programs. It also intercepts key combinations like control-c and control-z for job control. Further reading: https://en.m.wikipedia.org/wiki/POSIX_terminal_interface

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM