Interrupt BufferedInputStream.read()
I have a BufferedInputStream (using System.in as its underlying InputStream), that is blocking on a read() operation. At some point, I need to signal the thread that it is a part of to stop executing. However, if no more user input ever comes, then it will just sit there forever. How can I get around this?
# 2
Thanks for the reply. Interrupting the thread doesn't seem to do anything. It may just be my version of the Java implementation, it's supposedly pretty old. I'm not sure how to find out what version it is.
I don't have any control over it since its on an OCAP (Open Cable) platform.
Also, I don't see any read() that has a timeout for BufferedInputStream. *sigh* I hope I don't have to implement my own subclass.
Any ideas folks?
# 4
Yes, I have the thread handle for the thread that is waiting on BufferedInputStream.read() to return. I simply call myThreadHandle.interrupt(), correct?
My program is just a toy example to try out sockets. I read input from standard in and forward that to a server through a socket. Meaning, I have only a few threads, so I'm fairly confident that I'm doing it correctly.
# 6
> Read the socket with a read timeout and check
> Thread.currentThread().isInterrupted() every time you
> get the timeout.
My problem isn't reading the socket (though I suppose the interface is the same), its reading standard in (System.in). I don't see any InputStream subclasses that allow a timeout on a read operation. What classes implement this timeout?
# 11
Because using it in this way to implement interruptibility on a non-interruptible read is the only correct and useful usage of it I've ever encountered. Most people use it to guess the length of a file, which is contra its specification, or to allocate byte arrays when reading a socket, ditto, or to implement a read timeout on a socket when there is already a read timeout, which is just a waste of time and space.
So you would have something like this:
while (true)
{
if (input.available() > 0)
{
// Do a read and return the result
}
try
{
Thread.sleep(2000);
}
catch (InterruptedException exc)
{
// return interrupt status
}
}
ejpa at 2007-7-12 20:08:32 >
