为什么在 java.io.InputStream 中只有 mark() 和 reset() 方法是同步的?

编程入门 行业动态 更新时间:2024-10-27 18:29:24
本文介绍了为什么在 java.io.InputStream 中只有 mark() 和 reset() 方法是同步的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

不明白为什么 mark() 和 reset() 是同步的,为什么 read() 不同步?

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

推荐答案

java.io.InputStream 是一个抽象类.它有一个标记/重置的默认实现,只在重置时抛出异常,告诉不支持,因此不支持它的子类不需要编写自己的方法来抛出异常.同步"在默认情况下没有用,抛出异常.

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.

这篇关于为什么在 java.io.InputStream 中只有 mark() 和 reset() 方法是同步的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 14:09:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1410219.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   io   java   InputStream   reset

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!