Why only mark() and reset() method are synchronized in java.io.InputStream?
Asked Answered
M

1

8

Don't understand why mark() and reset() are synchronized and why read() not?

Muffle answered 11/6, 2015 at 8:3 Comment(3)
Presumably because races can occur between mark and reset, but not read.Normalize
It's a bit strange considering that those methods don't do anything InputStream. If you look at ByterrayInputStream which supports mark/reset, you will see that most methods are synchronized, although the mark method is not (which is a bit of a puzzle to me)...Zebulun
The read method for the BufferedInputStream is synchronizedHolloway
Q
4

java.io.InputStream is an abstract class. It has a default implementation for mark/reset that only throw an exception on reset telling that is not supported so subclasses that don't support it don't need to code their own method throwing the exception. "synchronized" is not useful for the default case, to throw an exception.

Any subclass that supports it will have to override those methods and synchronization is not inherithed so the overriden methods may or may not be synchronized.

I think it does not have any effect.

I guess it is a design flaw without consequences or maybe it is a warning so programmers that subclass it to synchronize those methods too because it should be made that way.

Quillon answered 11/6, 2015 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.