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?

[314 byte] By [James_Shepparda] at [2007-11-27 8:20:15]
# 1
Either use a from of read() that has a timeout and periodically check for a flag to quit, or else interrupt the thread which should cause the read() to throw an InterruptedException
dannyyatesa at 2007-7-12 20:08:32 > top of Java-index,Core,Core APIs...
# 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?

James_Shepparda at 2007-7-12 20:08:32 > top of Java-index,Core,Core APIs...
# 3
Are you sure you're interrupting the right thread?
dannyyatesa at 2007-7-12 20:08:32 > top of Java-index,Core,Core APIs...
# 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.

James_Shepparda at 2007-7-12 20:08:32 > top of Java-index,Core,Core APIs...
# 5
Read the socket with a read timeout and check Thread.currentThread().isInterrupted() every time you get the timeout.
ejpa at 2007-7-12 20:08:32 > top of Java-index,Core,Core APIs...
# 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?

James_Shepparda at 2007-7-12 20:08:32 > top of Java-index,Core,Core APIs...
# 7
None.
ejpa at 2007-7-12 20:08:32 > top of Java-index,Core,Core APIs...
# 8
Right. So, how would I implement a timeout on a read?
James_Shepparda at 2007-7-12 20:08:32 > top of Java-index,Core,Core APIs...
# 9
No idea. InputStream.read() isn't specified to be interruptible either. You could try the dreaded InputStream.available() and sleeping instead of just blocking in the read.
ejpa at 2007-7-12 20:08:32 > top of Java-index,Core,Core APIs...
# 10
> No idea. InputStream.read() isn't specified to be> interruptible either. You could try the dreaded> InputStream.available() and sleeping instead of just> blocking in the read.Why is it "dreaded"?
James_Shepparda at 2007-7-12 20:08:32 > top of Java-index,Core,Core APIs...
# 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 > top of Java-index,Core,Core APIs...