Java Concurrency
Java Concurrency
Golden Rules
- Make it right
- Make it fast
Approach for this
- Try to move the locks as lower as possible
- Try to be thread-safe (use only immutable or no states at all)
KeyWords: Race Condition: Multiple threads are trying to acquire the resources.
Data Race: Typically check and update kind of cases where, the date that got modified is up for access immidiatly for update and read.
Main components
- Executor
- ExecutorService
- ScheduledExecutorService
- Future
- CountDownLatch
- CyclicBarrier
- Semaphore
- ThreadFactory
- BlockingQueue
- DelayQueue
- Locks
- Phaser
Life Cycle of Thread
Java.util.concurrent
The Interfaces in this package are classified on prilimnary levels as collection related and concurrency tools & mechanisms:
Collection Related Interfaces |
---|
TransferQueue |
ConcurrentMap |
ConcurrentNavigableMap |
BlockingDeque |
BlockingQueue |
The classes in this package are classified on prilimnary levels collection and concurrency tools & mechanisms;
Concurrency Handling | Concurrent Collection |
---|---|
AbstractExecutorService | ArrayBlockingQueue |
CompletableFuture | ConcurrentHashMap |
CountDownLatch | ConcurrentHashMap.KeySetView |
CountedCompleter | ConcurrentLinkedDeque |
CyclicBarrier | ConcurrentLinkedQueue |
Exchanger | ConcurrentSkipListMap |
ExecutorCompletionService | ConcurrentSkipListSet |
Executors | CopyOnWriteArrayList |
ForkJoinPool | CopyOnWriteArraySet |
ForkJoinTask | DelayQueue |
ForkJoinWorkerThread | LinkedBlockingDeque |
FutureTask | LinkedBlockingQueue |
Phaser | LinkedTransferQueue |
RecursiveAction | PriorityBlockingQueue |
RecursiveTask | SynchronousQueue |
ScheduledThreadPoolExecutor | |
Semaphore | |
ThreadLocalRandom | |
ThreadPoolExecutor | |
ThreadPoolExecutor.AbortPolicy | |
ThreadPoolExecutor.CallerRunsPolicy | |
ThreadPoolExecutor.DiscardOldestPolicy | |
ThreadPoolExecutor.DiscardPolicy |
Lets take a closer look at those in Concurrency Handling column and try to re-classify them into differnt groups.
Collection
Future Flow
Executor Flow
CountDownLatch
Exchanger
Phaser
Remaining tools
- CyclicBarrier
- Executors
- Phaser
- Semaphore
- ThreadLocalRandom
- ForkJoinWorkerThread
- CompletionService