1/30
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a deadlock?
A situation where two or more processes wait forever for each other to release resources, so nothing progresses.
What is lockout (or starvation)?
When one process is perpetually denied access to a resource while others continuously get it.
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 is synchronization?
ensures only one thread can access a critical section (shared code/data) at a time.
used for preventing race conditions
can be done by:
synchronize methods
synchronize blocks
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 --
?
not atomic
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.
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?
visibility of a variable between threads, without locking
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
.
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.
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();
Does ReadWriteLock
prevent reader lockout of writers?
No – readers can still lock out writers indefinitely if new readers continuously arrive (a theoretical lockout issue)
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
pros and cons of synchronization
pros:
built into java
ensures mutual exclusion
cons:
doesn’t allow concurrent reads
only one thread can access the synchronized block at a time
pros and cons of locks
pros:
built into java
multiple readers allowed at the same time
longest waiting writer goes first
cons:
can still suffer writer lockout if readers keep coming
pros and cons of cloning
pros:
no synchronization needed
simplifies thread management
cons:
uses more memory
changes don’t effect shared data