Concurrency Flashcards

(44 cards)

1
Q

Synchronization

A

It is a JVM feature that ensures that two or more concurrent threads dont simultaneously execute a critical section.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Visibility

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Deadlock

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Livelock

A

Thread x keeps retrying an operation that will always
fail. It cannot make progress for this reason.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Starvation

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

🔒 ReentrantLock

A

A mutual exclusion lock with advanced features like try-lock, fairness, and interruptible lock acquisition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Semaphore

A

Controls access to a resource via a set number of permits. Think of it as a counter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

CountDownLatch

A

Waits until a certain number of events (threads) complete before continuing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

CyclicBarrier

A

Waits until a group of threads reach a common barrier point, then lets all proceed. Can be reused.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Phaser

A

A flexible synchronizer for phased tasks (like CyclicBarrier but more dynamic).
dynamic version of CyclicBarrier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Exchanger

A

Allows two threads to exchange data at a synchronization point.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

SynchronousQueue

A

A queue with zero capacity — each insert must wait for a remove. Used for handoffs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Core Java Concurrency Classes

A
  1. Executors and Thread Pools
  2. Task Interfaces
  3. Synchronization Utilities
  4. Concurrent Collections
  5. Locks and Synchronizations
  6. Atomic Variables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Executors and Thread Pools

A

-Executor
-ExecutorService
-ScheduledExecutorService​

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Task Interfaces

A

Define tasks for concurrent execution.

-Runnable
-Callable
-Future
-CompletableFuture

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Synchronization Utilities

A

-CountDownLatch
-CyclicBarrier
-Semaphore
-Exchanger
-Phaser

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Concurrent Collections

A

ConcurrentHashMap
CopyOnWriteArrayList
ConcurrentLinkedQueue
BlockingQueue
DelayQueue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Locks and Synchronizers

A

ReentrantLock
ReadWriteLock
StampedLock
LockSupport​

19
Q

newSingleThreadExecutor()

A

Creates executor that uses a single worker thread operating off an unbounded queue

20
Q

newFixedThreadPool(int nThreads)

A

Creates a thread pool with a fixed number of threads.

21
Q

newCachedThreadPool()

A

Creates a thread pool that creates new threads as needed and reuses previously constructed threads when available.

22
Q

newScheduledThreadPool(int corePoolSize)

A

Creates a thread pool that can schedule commands to run after a delay or periodically.

23
Q

newSingleThreadScheduledExecutor()

A

Creates a thread pool that can schedule commands to run after a delay or periodically.
Same as above, but uses only one thread.

24
Q

newWorkStealingPool()

A

Creates a work-stealing thread pool using multiple queues (Java 8+).

25
execute(Runnable) vs submit(Callable)
-no return value -returns a Future Runnable has no return value. Only Callable can return a result.
26
Runnable short summary
No result, no exception handling. Just "run it." ❌ No return value ❌ No throwing checked exception ❌ No chaining ✅ Supports Asynch
27
Callable short summary
Runnable’s Smarter Cousin ✅ Returns value ✅ Throws Checked exception ✅ Suppots Asynch ❌ No chaining Use Callable when you want the task to return a result or throw exceptions.
28
Future short summary
The Promise of a Result ❌ No chaining ❌ No Asynch Support (Not native) ✅ Returns value ✅ Throws Checked exception -get()
29
CompletableFuture short summary
Non-blocking, reactive-style async programming. ✅ Returns Value ✅ Throws Checked Exception ✅ Asynch Support ✅ Chaining
30
ConcurrentHashMap
The most commonly used atomic collection. Allows thread-safe read and write operations. Segmented internally for high performance under concurrency.
31
CopyOnWriteArrayList
Thread-safe version of ArrayList. Best for read-heavy, write-rarely use cases. On every mutation, it creates a new copy of the array.
32
CopyOnWriteArraySet
Backed by CopyOnWriteArrayList. Ensures no duplicates. Safe iteration without ConcurrentModificationException.
33
ConcurrentLinkedQueue
A non-blocking, thread-safe FIFO queue. Ideal for high-concurrency environments. Based on a lock-free algorithm using CAS (Compare-And-Swap).
34
ConcurrentLinkedDeque
Same as ConcurrentLinkedQueue but double-ended (can add/remove from both ends).
35
LinkedBlockingQueue LinkedBlockingDeque
Blocking versions of queues and deques. Support blocking take/put operations using wait() internally. A FIFO (First-In-First-Out) blocking queue backed by a linked list. Thread-safe Allows blocking put() and take() Used in producer-consumer designs
36
When to Use Atomic Classes
-You need lightweight, lock-free thread safety. -Replacing synchronized or volatile for performance. -You want low-level control over concurrency (e.g., counters, flags, shared data). -You're building custom concurrent structures.
37
AtomicInteger
A thread-safe integer that supports atomic operations like incrementAndGet(), compareAndSet(), etc. Use Case: Atomic counters
38
39
AtomicLong
Same as AtomicInteger, but for long values. Useful for unique IDs, timestamps, counters across threads.
40
AtomicBoolean
-Useful when you want to track a flag atomically — like a toggle or a running status. -Great for one-time initialization or ensuring a task runs once.
41
AtomicReference
Allows atomic operations on object references. Great for immutability, shared mutable state, or custom compare-and-set logic.
42
AtomicStampedReference / AtomicMarkableReference
These help with versioning or avoiding ABA problems in lock-free algorithms. AtomicStampedReference → Holds a reference and a version number (stamp). AtomicMarkableReference → Holds a reference and a boolean flag (mark). Advanced use in lock-free data structures, not common in business apps.
43
🔧 What is Compare-And-Swap (CAS)?
CAS is a low-level atomic instruction used to safely update a variable in memory only if it hasn't been changed by another thread. It's like saying: "I'll only set this value if it's still what I last saw." Imagine you're updating a whiteboard score with a marker: You read the score: 42. You go to write 43, but before you do, someone else changes it to 44. Your update would be invalid, because the original value isn’t 42 anymore. So you recheck the value before updating — that's CAS.
44
What compareAndSet(10, 11) does:
If current value is 10, it sets it to 11. If current value is not 10, it does nothing and returns false.