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

守給你的承諾、 提交于 2020-01-14 07:51:10

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/30775004/why-only-mark-and-reset-method-are-synchronized-in-java-io-inputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!