Concurrency Flashcards
(44 cards)
Synchronization
It is a JVM feature that ensures that two or more concurrent threads dont simultaneously execute a critical section.
Visibility
It ensures that a thread executing in a critical section always sees the most recent changes to shared variables.
It reads these variables from main memory on entry to the critical section and writes their values to main memory on exit.
Deadlock
Thread 1 waits for a resource that thread 2 is holding
exclusively and thread 2 is waiting for a resource that thread 1 is holding exclusively. Neither thread can make progress.
Livelock
Thread x keeps retrying an operation that will always
fail. It cannot make progress for this reason.
Starvation
Thread x is continually denied (by the scheduler)
access to a needed resource in order to make progress. Perhaps the scheduler executes higher-priority threads before lower-priority threads and there is always a higher-priority thread available for execution. Starvation is also commonly referred to as
indefinite postponement.
🔒 ReentrantLock
A mutual exclusion lock with advanced features like try-lock, fairness, and interruptible lock acquisition.
Semaphore
Controls access to a resource via a set number of permits. Think of it as a counter.
CountDownLatch
Waits until a certain number of events (threads) complete before continuing.
CyclicBarrier
Waits until a group of threads reach a common barrier point, then lets all proceed. Can be reused.
Phaser
A flexible synchronizer for phased tasks (like CyclicBarrier but more dynamic).
dynamic version of CyclicBarrier
Exchanger
Allows two threads to exchange data at a synchronization point.
SynchronousQueue
A queue with zero capacity — each insert must wait for a remove. Used for handoffs.
Core Java Concurrency Classes
- Executors and Thread Pools
- Task Interfaces
- Synchronization Utilities
- Concurrent Collections
- Locks and Synchronizations
- Atomic Variables
Executors and Thread Pools
-Executor
-ExecutorService
-ScheduledExecutorService
Task Interfaces
Define tasks for concurrent execution.
-Runnable
-Callable
-Future
-CompletableFuture
Synchronization Utilities
-CountDownLatch
-CyclicBarrier
-Semaphore
-Exchanger
-Phaser
Concurrent Collections
ConcurrentHashMap
CopyOnWriteArrayList
ConcurrentLinkedQueue
BlockingQueue
DelayQueue
Locks and Synchronizers
ReentrantLock
ReadWriteLock
StampedLock
LockSupport
newSingleThreadExecutor()
Creates executor that uses a single worker thread operating off an unbounded queue
newFixedThreadPool(int nThreads)
Creates a thread pool with a fixed number of threads.
newCachedThreadPool()
Creates a thread pool that creates new threads as needed and reuses previously constructed threads when available.
newScheduledThreadPool(int corePoolSize)
Creates a thread pool that can schedule commands to run after a delay or periodically.
newSingleThreadScheduledExecutor()
Creates a thread pool that can schedule commands to run after a delay or periodically.
Same as above, but uses only one thread.
newWorkStealingPool()
Creates a work-stealing thread pool using multiple queues (Java 8+).