`
wjm251
  • 浏览: 110250 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

<Java Concurrency in Practice>笔记1

    博客分类:
  • java
阅读更多
之前的部分都没有记录了,从这里开始,所看过的自己意识中并非理所当然的东西记录一下,以后再看看笔记就复习一遍。

4.5. Documenting Synchronization Policies
应该在文档中明确表明wether our class is thread safe,so our user or maintainer will be feel better.
Some library code like JDBC or Servlet framwork does not specific whether they are threadsafe, from a designer's perspective(not merely a user's),  we can assume them does this, we can resonable assume,servlet must be designed to be accessed by multiple thread,otherwise it will be useless.
同样的道理DataSource.getConnection一般是线程安全的,但connection本身可能不适合被多线程同时使用。通常应用都有一个池来管理,或者把connection放到线程限制当中

5.1. Synchronized Collections
给已经线程安全的容器增加功能时,如vector,要用容器对象来做同步(容器对象本身作为自己内部同步的锁)
迭代的时候如果不同步并且迭代中被修改的话,是快速失败的,会抛出ConcurrentModificationException
注意隐式的迭代被调用,比如容器对象的toString调用时,会隐式迭代
synchronized collections 对所有的操作加锁,性能问题。
5.2. Concurrent Collections
在并发高性能,高scalability方面,是对Synchronized Collections的替代
The iterators returned by ConcurrentHashMap are weakly consistent instead of fail-fast.
CopyOnWriteArrayList在读取操作远远大于修改时非常适用,比如事件通知系统中的监听器列表。他在修改的时候会复制一个出来,而不修改原来的。

5.3. Blocking Queues and the Producer-consumer Pattern
   One of the most common producer-consumer designs is a thread pool coupled with a work queue; this pattern is embodied in the Executor task execution framework that is the subject of Chapters 6 and 8.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics