1/40
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is the Readers and Writers problem?
Example:
One client reads while another deletes
How would concurrency be prevented in the bank example?
By ensuring one withdrawal completes entirely before another begins (i.e. making it atomic).
What is a deadlock?
A situation where two or more processes wait forever for each other to release resources, so nothing progresses.
What is livelock?
When threads keep changing state to avoid deadlock, but still make no progress (e.g., both step aside repeatedly).
What is lockout (or starvation)?
When one process is perpetually denied access to a resource while others continuously get it.
Can multiple readers access data simultaneously?
Yes, if no one is writing.
Can multiple writers write at the same time?
No, only one writer can modify data at a time.
What does “atomic” mean in the context of concurrency?
An operation that runs completely without interruption.
Is regular Java code atomic by default?
No – even a simple line like x = a + b;
can be interrupted mid-execution.
What two main concurrency tools does Java offer for managing concurrency?
Monitors using synchronized
, wait
, notify
, notifyAll
Read-write locks from the Java concurrency library (ReenrantReadWriteLock)
What does the synchronized
keyword do in Java?
It ensures that only one thread at a time can execute the synchronized method or block on a given object.
What is a MUTEX?
Short for mutual exclusion – it ensures that only one thread can access a critical section of code at a time.
Why must threads share an object to synchronize properly?
Because synchronization applies per object – if threads don’t use the same object, they won’t block each other.
What is the problem with unsynchronized operations like ++
and --
?
They are not atomic and can be interrupted by other threads, leading to incorrect results.
What is the purpose of a synchronized block?
To allow fine-grained control by locking on a specific object for part of a method, rather than the whole method.
What strategy can prevent writes while reads are happening?
Use a synchronized counter to track active readers. Only allow writes when the counter is zero.
Why can’t we check if (counter == 0)
outside a synchronized block?
Because another thread might change the counter before we enter the block. We must check inside the synchronized block.
What does wait()
do inside a synchronized block?
It pauses the thread and releases the lock, allowing others to proceed.
What does notifyAll()
do?
It wakes up all waiting threads that have called wait()
on the same object.
Why use notifyAll()
instead of notify()
?
Because notify()
wakes just one thread (which might not be able to proceed), possibly causing deadlock.
Where should notifyAll()
be called in readers-writers logic?
When a reader finishes and the counter becomes zero, allowing pending write threads to proceed.
What’s the correct reader thread structure?
counter.inc();
Perform read
counter.dec();
(if c == 0
, call notifyAll()
)
What is the volatile
keyword used for?
It prevents caching and ensures visibility and ordering of variable changes across threads.
Why don't we need volatile
in the standard readers-writers pattern?
Because we already use synchronized blocks, which enforce memory consistency.
When can you use just wait()
inside a method (without an object)?
In a synchronized method, where the method implicitly synchronizes on this
.
What is the alternative solution for the readers and writers problem?
Cloning
Clone the shared list during read operations, and only synchronize the clone operation and writes.
Is cloning inefficient?
No – it's a shallow copy, meaning only references are copied, not the objects themselves
Why is the cloning approach efficient?
Because the critical section (synchronized part) is very short – only as long as the cloning takes.
What is ConcurrentHashMap
?
A thread-safe Java collection that supports concurrent read and write access without locking the whole map.
What is the benefit of putIfAbsent()
in ConcurrentHashMap
?
It’s an atomic operation that safely inserts a key-value pair only if the key is not already present.
What mistake can occur when combining putIfAbsent()
with logic like conditionals?
Logic like if (englishOnly)
can be interrupted or changed between the check and the putIfAbsent()
call, causing race conditions.
What is ReadWriteLock
used for?
It allows multiple readers to access shared data simultaneously, but ensures exclusive access for writers.
How do you create a read-write lock in Java?
ReadWriteLock lock = new ReentrantReadWriteLock();
What is the syntax to acquire and release a read lock?
lock.readLock().lock();
// ... read operations ...
lock.readLock().unlock();
What happens when a thread acquires a read lock?
It can read data if no thread holds the write lock
Other threads can also read simultaneously if they also hold the read lock
What happens when a thread acquires a write lock?
No other thread can read or write while the write lock is held
It blocks all read and write operations until released
Does ReadWriteLock
prevent reader lockout of writers?
No – readers can still lock out writers indefinitely if new readers continuously arrive (a theoretical lockout issue)
What advantage does ReadWriteLock
offer over manual counters?
It hides the complexity of counting readers/writers and automatically handles synchronization.
How does ReadWriteLock
internally work?
It uses synchronized counters to track read locks and waits for them to reach zero before allowing a write lock.
Does ReadWriteLock
guarantee fairness between writers?
Yes – if multiple writers are waiting, the one waiting longest goes first.
What are the 4 ways to prevent concurrency issues?
Locking
Cloning
Counters
Synchronizing