• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stopping a thread that's trying to read from stdin  RSS feed

 
Andrew Keidel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'd like to read lines of user input for one minute and then stop reading them and do other stuff.
I don't want to use any deprecated methods, such as stop(), to implement this.
Problem is, read(), readLine(), readUTF(), and other such methods block, so that even after a minute has expired, they still wait for one more line of input before ending.
I also tried making a separate Timer() thread and setting a "read" flag from true to false after a minute passes; the idea there was to only read if the flag is true. I get the same problem, though, because once I start reading, I can't stop without user input. (So I can't stop after exactly one minute.)
A third attempt was to use available() or ready(), but these methods didn't work the way I wanted them to. For instance, using InputStream.available() on an InputStream worked in that I could stop after exactly one minute, because I could check the "read" flag and the availability in a while loop and then see why the loop terminated. The problem, though, is that using available() requires that I read on character at a time, and so the input on the screen doesn't show up as the user types it. It all shows up after hitting enter instead.
Using ready(), the problem was that the read() methods were still blocking.
So does anyone know of an easy way to read input from the command line for a specified amount of time? Doesn't seem that this should be difficult.
[ January 07, 2002: Message edited by: Andrew Keidel ]
 
Laudney Ren
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you can construct a thread as an innerclass and makes it sleep for 1 minute before invokes the close() method of the inputStream
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!