This repository contains my practice and learning materials on the topic of Java Concurrency. The code is structured around various core concepts and advanced mechanisms provided by the Java concurrent API.
- Creating and managing threads
- Daemon threads (
KitchenDemonDemo) - Data races and basic synchronization (
DataRaceDemo) - Atomic variables (
AtomicVariableDemo)
- Using
ExecutorService(ExecutorServiceSingleThread,UsingExecutorService) - Exploring
CallableandFuturefor async execution (DemoUsingFutureCallable) - Scheduled thread pools (
ScheduledThreadPool) - Cached vs Fixed thread pools
- Explicit Locks:
ReentrantLock(ReentrantLockDemo),ReadWriteLock(ReadWriteLockDemo) - Methods like
tryLock()(TryLockDemo) - Avoiding abandoned locks
- Thread-safe Collections:
ConcurrentMap,CopyOnWritecollections,BlockingQueue - Synchronized Collections:
SyncMap,SyncSet - Avoiding
ConcurrentModificationException
- CountDownLatch: Synchronizing one or more threads to wait for a set of operations (
CountDownLatchDemo) - Barriers and Latches
- Implementation using basic Mutexes
- Implementation using Semaphores
- Understanding and simulating Deadlocks
- Dealing with abandoned locks
- Divide and Conquer approach (
divideconquer/) - Futures
- Parallel algorithms like Sequential Matrix Multiplier
- Core Java memory model concepts
- Ensuring visibility with
volatile - Lock-free thread-safety with
Atomicclasses
Taking a problem, and then how to design and develop it into a parallel solution
A large program that is distributed across multiple physical systems.
Parallel Design Stages:
- Partitioning: breaking down a problem into discrete chunks that can be distributed as multiple tasks
- Domain decomosition: dividing the data associating with the problem; block decomposition or cyclic decomposition;
- Functional decomposition: decomposimg a big task according to sub-steps to carry out it in a whole.
- Combination of the both above.
- Communication: some task may be compeletly independent; however, sub-tasks may need to co-ordinate with other and need to communicate with other.
- Point-to-piont com
- sync. blocking com
- async. non-blocking com
- Overhead: computer time/time spent on communication
- Latency: time cost message traveling from A to B(ms)
- Bandwidth: Amount to data com per sec(GB/s)
- Agglomeration(a mass or collection of things; an assemblage)
- Granularity = computation/communication
- a fine-grained parallelism: a big number of small tasks; a good load balancing; downside: increasing com. decreasing computation-to-communication ratio
- a corse-grained parallelism: a small number of large tasks; high computation-to-communication ratio; downside: inefficient load balancing.
- Mapping: it means to find a distributed system to perform your task.