`
leonzhx
  • 浏览: 793457 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 70: Document thread safety

阅读更多

1.  In normal operation, Javadoc does not include the synchronized modifier in its output, and with good reason. The presence of the synchronized modifier in a method declaration is an implementation detail, not a part of its exported API. It does not reliably indicate that a method is thread-safe. The claim that the presence of the synchronized modifier is sufficient to document thread safety embodies the misconception that thread safety is an all-or-nothing property. In fact, there are several levels of thread safety. To enable safe concurrent use, a class must clearly document what level of thread safety it supports.

 

2.  The following list summarizes levels of thread safety:

    1)  immutable—Instances of this class appear constant. No external synchronization is necessary.

    2)  unconditionally thread-safe—Instances of this class are mutable, but the class has sufficient internal synchronization that its instances can be used concurrently without the need for any external synchronization.

    3)  conditionally thread-safe—Like unconditionally thread-safe, except that some methods require external synchronization for safe concurrent use. Examples include the collections returned by the Collections.synchronized wrappers, whose iterators require external synchronization.

    4)  not thread-safe—Instances of this class are mutable. To use them concurrently, clients must surround each method invocation (or invocation sequence) with external synchronization of the clients’ choosing. 

    5)  thread-hostile—This class is not safe for concurrent use even if all method invocations are surrounded by external synchronization. Thread hostility usually results from modifying static data without synchronization.

 

3.  If an object represents a view on some other object, the client generally must synchronize on the backing object, so as to prevent its direct modification. For example, the documentation for Collections.synchronizedMap says this: It is imperative that the user manually synchronize on the returned map when iterating over any of its collection views:

Map<K, V> m = Collections.synchronizedMap(new HashMap<K, V>());
...
Set<K> s = m.keySet(); // Needn't be in synchronized block
...
synchronized(m) { // Synchronizing on m, not s!
  for (K key : s)
    key.f();
}

 

4.  If you write an unconditionally thread-safe class, consider using a private lock object in place of synchronized methods. This protects you against synchronization interference by clients and subclasses and gives you the flexibility to adopt a more sophisticated approach to concurrency control in a later release.

 

5.  Every class should clearly document its thread safety properties with a carefully worded prose description or a thread safety annotation. The synchronized modifier plays no part in this documentation. Conditionally thread-safe classes must document which method invocation sequences require external synchronization, and which lock to acquire when executing these sequences.

 

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 82: Document thread safety Item 83: Use lazy initialization judiciously Item 84: Don’t depend on the thread scheduler 12 Serialization Item 85: Prefer alternatives to Java serialization Item 86:...

    理论实操:document对象与点击事件

    理论实操:document对象与点击事件

    JS:document.all的用法

    然而,需要注意的是,`document.all`在标准浏览器中可能不受支持,因此在实际项目中,更推荐使用`document.getElementById`、`document.getElementsByName`等标准方法来访问DOM元素,以确保代码的兼容性和可维护性。

    JavaScript笔记总结(Xmind格式):第七天

    DOM:document object model 文档对象模型 获取DOM节点(获取的元素都属于DOM对象):  1.根据ID获取元素:document.getElementById  2.根据类名获取元素:document.getElementsByClassName  3.根据标签名获取...

    The document was created with Spire.PDF for Java

    标题 "The document was created with Spire.PDF for Java" 提供了一个关键信息,即文档是使用Spire.PDF for Java工具创建或处理的。Spire.PDF for Java是一款由E-iceblue公司开发的PDF库,它允许Java开发者在应用...

    perl6-html-parser-xml:html-> xml :: document转换器

    该模块将读取HTML并尝试构建XML :: Document( )特征: 如果遇到某些其他标签,则自动关闭某些标签很好地解析脏HTML(AFAIK),如果不正确,则提交错误Perl6的魔力状态: 错误/功能请求维护模式用法: my $...

    webpack4 配置 ssr 环境遇到“document is not defined”

    ReferenceError: document is not defined 本次package.json使用版本信息: { vue-loader: ^15.4.2, mini-css-extract-plugin: ^0.4.3, webpack: ^4.20.2, webpack-cli: ^3.1.2 ... } 相关代码 问题原因: ...

    cocos2d-x 3.0 rapidjson 读取Json

    如果解析成功,`document`将是一个包含Json数据的DOM(Document Object Model)结构。 接下来,可以通过遍历DOM来读取Json数据。例如,获取"name"的值: ```cpp if (document.HasMember("name") && document["name...

    网页图片模糊插件blurify.js代码.zip

    images: document.querySelectorAll('.blurify1'), blur: 1, mode: 'auto', }); new blurify({ images: document.querySelectorAll('.blurify2'), blur: 2, mode: 'auto', });  new blurify({ images: ...

    Template_ Item Definition.doc

    1. 文档管理和配置控制信息(Document Management and Configuration Control Information) 这部分通常包括项目的版本控制、修订历史以及负责文档管理的人员信息。例如,"Revision Number: 2.0 - Status: Released...

    repostorie.jar

    javascript:document.getElementById('downLoad').action='/download.php?fileid=15775884';downishare('0');javascript:document.getElementById('downLoad').action='/download.php?fileid=15775884';downishare('...

    iTextSharp使用手册.pdf

    Document document = new Document(); 第二步,为该 Document创建一个 Writer 实例: PdfWriter.getInstance(document, new FileStream("Chap0101.pdf", FileMode.Create)); 第三步,打开当前 Document document...

    用js小类库获取浏览器的高度和宽度信息

    网页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offsetWidth + ” (包括边线和滚动条的宽)”; 网页可见区域高:document.body....

    JavaScript中document用法小结

    JavaScript 中 document 对象用法小结 JavaScript 中的 document 对象是一个非常重要的对象,它提供了大量的属性和方法来操作 HTML 文档。下面是对 document 对象的一些常用属性和方法的总结。 属性 1. `document...

    SIMATIC STEP 7 Safety Advanced V15 .pdf

    Readme 文件提供了STEP 7 Safety Advanced V14的详细信息,可以在portable document format(.pdf)中查看:ReadmeFAIL_de_en_V15.pdf(247.9 KB)。 下载文件 * STEP_7_Safety_V15.exe(347.6 MB):试用版本的...

    devexpress documentManager demo

    DocumentManager是DevExpress提供的一个组件,主要用于管理和展示文档。在这个“devexpress documentManager demo”中,我们可以深入理解如何在应用中集成和操作文档管理功能。 1. **DocumentManager组件**:...

    introduction document - Safety stock in SAP

    介绍安全库存在SAP中的几种运行方式:安全库存安全库存部份可用动态安全库存

    ember-css-modules-active-route:当路由处于活动状态时,`:app-root`和`:document-root`选择器将样式应用于根元素

    :app-root和:document-root选择器,可在路由处于活动状态时将样式应用于根元素。 安装 ember install ember-css-modules-active-route ember-css-modules 这是的插件,因此您也需要安装它。 用法 :app-root :映射...

    总结常用JavaScript语法107条

    1.输出语句:document.write("") 2.JS中的注释为:// 3.传统的HTML文档顺序是:document-&gt;html-&gt;(head,body) 4.一个浏览器窗口中的DOM顺序是:window-&gt;(navigator,screen,history,location,document) 5.得到表单中元素的...

    LaTeX: A Document Preparation System part2

    LaTeX: A Document Preparation System 2nd edition part2 货真价实

Global site tag (gtag.js) - Google Analytics