Effective Java 3/E summary chanpter 10 (Shorter version)
12 Apr 2021Chanpter 10 : Concurrency
You can find the longer version of this summary here.
Concurrency satisfaction
- Exclusive execution:
synchronizedkeyword - Communication between threads:
volatilefield, both read and write sychronization
Synchronization failure
- Unresponsive, safety failure
- Avoid calling alien methods
- Minimize the synchronization area
Executor rather than thread, use task, use concurrency utility
- Normally use
newCachedThreadPool - Use
newFixedThreadPoolon heavy servers - If you want full control, use
ThreadPoolExecutorclass directly ConcurrentHashMap,BlockingQueue,CountDownLatch,Semaphore,Phaser
Documentation
- Immutable: No need to sync (
@Immutable) - Unconditional thread safety: No need to sync (
@ThreadSafe) - Conditional thread safety: synchronization for some methods (
@ThreadSafe) - Not thread safe: should be wrapped with an external synchronization mechanism (
@NotThreadSafe)
Caution
- Avoid using lazy initialization (
synchroized,volatile) - Avoid priority for
Thread(usingThread.yield)