Java – sets the timeout for user input

Can I set a timer for user input? Wait 10 seconds – proceed to the next step, etc

//wait several seconds{
 String s = new BufferedReader(new InputStreamReader(system.in)).readLine();
 //wait server seconds}
 //next operation and etc.

Solution

Out of the box, No Usually, when another thread closes the underlying stream or reaches the end of the input, the reader will only interrupt the read () call

Since read () is not all interruptible, this becomes a concurrent programming problem A thread that knows the timeout needs to be able to interrupt the thread trying to read input

Essentially, the read thread must poll the reader's ready () method instead of locking read () when nothing is readable If in Java util. concurrent. If this polling and waiting operation is wrapped in future, the get () method of future is called with timeout

This article describes in detail: http://www.javaspecialists.eu/archive/Issue153.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>