3

Can't seem to find anything about input stream "blocking" that describes both what it is and when it occurs. Is this some type of multi-thread prevention of concurrent threads accessing the same stream?

On that note, when two concurrent threads access the same stream at the same time, can this cause problems, or do both threads get their own stream pointers? Obviously, one would need to wait, but hopefully it wouldn't lead to an unchecked exception.

3 Answers 3

5

"Blocking" is when a read or write hangs, while waiting for either more information (for reads) or for more space in some internal buffer (for writes) before returning control to the calling thread.

And I'm pretty sure the stream object takes care of its own read/write locations, so the pointer just points to the stream object, which reads out of its own buffer. So, if you're reading with synchronized methods, then each read will wait its turn, and get cohesive (but not overlapping) data. If the methods aren't synchronized, then I'm pretty sure all hell will break loose.

Sign up to request clarification or add additional context in comments.

Comments

3

In the context of input streams, "blocking" typically refers to the stream waiting for more data becoming available. The term would probably make more sense if you think about sockets rather than files.

If you have multiple threads concurrently reading from the same stream, you have to do your own synchronization. There are no thread-specific "stream pointers". Again, think about multiple threads reading from the same socket (rather than from a file).

2 Comments

So if thread 1 began reading data from the beginning of a stream, and then (afterwards) thread 2 began reading from the same stream, would thread 2 read from the beginning, or where thread 1 left off?
@aix since the streams are not necessarily thread-safe, the answer is, "you cannot say for sure" :) I mean, you're right iff read in thread 1 happens-before read in thread 2.
2

Each stream has a stream pointer. It doesn't make much sense to have two threads reading the same stream.

1 Comment

I see what you're saying. I think I may have worded it wrong, or maybe come up with the entire thing wrong in my head. Sometimes I confuse even myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.