为什么在 java.io.InputStream 中只有 mark() 和 reset() 方法是同步的? [英] Why only mark() and reset() method are synchronized in java.io.InputStream?

查看:34
本文介绍了为什么在 java.io.InputStream 中只有 mark() 和 reset() 方法是同步的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不明白为什么 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆