都是Java属性吗?方法完全同步?
我知道Properties类是Hashtable的子类。那么所有继承的方法都是同步的,但是Properties的其他方法如store、load等呢? (专门针对 Java 1.6)
I know that the Properties class is a sub-class of Hashtable. So all the inherited methods are synchronized, but what about the other methods of Properties such as store, load, etc? (Dealing specifically with Java 1.6)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
java1.6 javadoc 说:
the java1.6 javadoc says:
我总是发现文档免责声明具有误导性,特别是对于初学者(如果不是您的情况,请原谅)。
此类是线程安全的:多个线程可以共享单个 Properties 对象,而无需外部同步。
即使是线程安全类也比您想象的更需要同步。这些类上同步的是它们的方法,但用户通常在更复杂的上下文中使用此类。
如果你只 put/get 就可以了,但是如果有更多的代码,事情就会变得更加严格:
如果该部分是同步块,则此示例代码仅在多线程环境中打印 shure“abc123”(即使这样,事情也可能会出错) )。
出于这个原因(当然还有性能),我更喜欢非线程安全类,并且我被迫思考:我的程序线程安全吗?
I always found the doc disclaimer misleading, specially for beginners (pardon if it is not your case).
This class is thread-safe: multiple threads can share a single Properties object without the need for external synchronization.
Even Thread-safe classes need synchronization more than you think. What is synchronized on that classes are their methods, but often a user uses this classes in a more complex context.
If you only put/get it is ok, but with some more code things get tighter:
This example code only prints for shure "abc123" in a multi threaded environment, if the section is a synchronized block (and even then things could get wrong).
For that reason (and of courrse performance) i prefer non thread safe classes and i get forced to think: is my program thread safe ...