關于java多線程的幾個小貼士
原文地址:
http://weblogs.java.net/blog/caroljmcdonald/archive/2009/09/17/some-java-concurrency-tips
大綱:
Prefer immutable objects/data
盡可能使用不變對象/數據
Threading risks for Web applications
注意web應用的線程安全問題
Hold Locks for as short a time as possible
持有鎖的時間盡可能短
Prefer executors and tasks to threads
盡可能使用JDK并發工具包提供的Executor框架,進行多線程操作
Prefer Concurrency utilities to wait and notify
盡可能使用JDK并發工具包提供的工具進行同步(等待和通知)
- Concurrent Collections
- ConcurrentMap
- ConcurrentHashMap
- COncurrentLinkedQueue
- CopyOnWriteArrayList
- BlockingQueue Implementations
- ArrayBlockingQueue
- LinkedBlockingQueue
- PriorityBlockingQueue
Producer Consumer Pattern
了解生產者消費者模式
Synchronizers
同步器
- Semaphore
- CountDownLatch
- CyclicBarrier
- Exchanger
Multithreaded Lazy Initialization is tricky
多線程環境下,lazy init是一件棘手的事情
Prefer Normal initialization
盡可能使用正常的初始化(盡可能不要使用lazy init)