William the Coderer
William the Coderer

Reputation: 708

Java input stream "blocking" and multithreading

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.

Upvotes: 3

Views: 3952

Answers (3)

FrankieTheKneeMan
FrankieTheKneeMan

Reputation: 6800

"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.

Upvotes: 5

NPE
NPE

Reputation: 500357

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).

Upvotes: 3

user207421
user207421

Reputation: 310907

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

Upvotes: 2

Related Questions