About Me

我的相片
Mr. Pigg
Taipei, Taiwan
檢視我的完整簡介

星期一, 8月 11, 2008

Effective Java - Chapter 11 Serialization

0 意見

defaultReadObject/defaultWriteObject

  1. Serialization
    - serialized form and UID
    - another constructor
    - 提供parameterless constructor以便subclass可以serializable, 用AtomicReference確保subclass有正確的initiate

  2. Use a custom serialized form
    - don't use default without consideration
    - 只寫出logic representation
    - 通常custom會較快
    - always invoke defaultReadObject/defaultWriteObject
    - Hashtable的default form甚至會error !!
    - always use serial version UID
  3. Write readObject methods defensively
  4. Use enum to instance control
  5. 當需要自己寫readObject/writeObject時, 可考慮使用Serialization Proxy

Effective Java - Chapter 10 Concurrency

0 意見

  1. Use Synchronize access to shared mutable data
    - synchronize除了用來防止同時更改資料,也是用來保證會讀取到最新資料
    - synchronize要設在get與update
    - volatile
  2. 不要過度使用Synchronize
    - alien => 只synchronize必要的部份, synchoronize越少越好 open call
    - 使用CopyOnWriteArrayList/Set
    - 讓client決定是否synchronize, 除非你自己有把握做的更好
    - static field一定要synchronize
  3. Prefer executors
    - ExecutorService提供許多好用的機制
  4. Prefer concurrency utilities to wait and notify
    - ExecutorService
    - Concurrent collections
    - CountDownLatch / Semaphore, CyclicBarrier / Exchanger
    - The standard idiom for using the wait method
    - Private lock object idiom
  5. Any program that relies on the
    thread scheduler for correctness or performance is likely to be nonportable
  6. thread groups are obsolete

Effective Java - Chapter 6 Enums & Annotations

0 意見

  1. Use enums instead of int constants
    - instance controlled
    - a generalization of singletons
    - 可提供各種物件化的programming
    - enum的method多被為private or package private
    - constant-specific method implementations
    - strategy enum pattern
    - valueOf, toString(), fromString()
  2. Use EnumSet, EnumMap
  3. Annotation
    - Retention
    - Target
    - Parameter
  4. Override
  5. Mark interface

星期五, 8月 01, 2008

Effective Java Chapter 9 Exception

0 意見

1. Use exception only for exceptional conditions
- Good API design 要提供讓client檢查狀態的API

2. checked exception and runtime exception

3. 盡量避免不必要的checked exception
- Client方便
- 可將checked轉成 unchecked

4. Favor use standard exception
- IllegalArgumentException
- IllegalStateException
- NullPointerException
- IndexOutOfBoundException
- ConcurrentModificationException
- UnsupportedOperationException

5. Throw exceptions appropriate to the abstraction
- exception translation
- exception channing

6. Document all excepion

7. Include failure-capture information in detail message
- 包裝有用的資訊
- 需要自己改寫Exception

8. 維持Exception的原子性
- Object immutable
- rollback
- check first

9. Don't ignore it