Chapter 13: Concurrency Flashcards
A process is a group of associated threads that execute in the same shared environment.
If false, why?
True.
A thread is the smallest unit of execution that can be scheduled by the operating system. If false, why?
True.
A single-threaded process contains exactly one thread, whereas a multi-threaded process supports more than one thread.
If false, why?
True.
A single process has shared memory between its threads.
If false, why?
True.
A task is a single unit of work performed by a thread.
If false, why?
True.
The property of executing multiple threads and processes at the same time is referred to as concurrency.
If false, why?
True.
Operating systems use a thread scheduler to determine which threads should be currently executing.
If false, why?
True
Local variables are shared among all threads within a process.
If false, why?
Local variables are stored on the stack and are thread-specific.
(False)
Static methods and variables are unique to each thread within a process.
If false, why?
Static methods and variables are shared among all threads in the same process.
(False)
A context switch occurs when a thread completes execution and terminates.
If false, why?
A context switch happens when a thread’s execution is paused to allow another thread to run.
(False)
A thread scheduler guarantees an equal amount of CPU time for every thread.
If false, why?
The scheduler may use different strategies, and priorities can affect execution time.
(False)
A thread cannot interrupt another thread, even if it has a higher priority.
If false, why?
A higher-priority thread can supersede a lower-priority thread in scheduling.
(False)
CPUs are directly connected to the OS thread scheduler.
If false, why?
The OS thread scheduler manages threads, but CPUs execute instructions independently.
(False)
A thread can execute multiple tasks at the same time.
If false, why?
A thread can only execute one task at a time but can switch between tasks.
(False)
What is the purpose of the AtomicInteger class in Java?
AtomicInteger is a class from java.util.concurrent.atomic that allows atomic (thread-safe) operations on int values without using synchronisation or locks
Why is AtomicInteger preferred over volatile int for concurrent updates?
Unlike volatile, which only ensures visibility, AtomicInteger ensures atomicity. It guarantees that compound operations like increment are executed as a single atomic step, avoiding race conditions
What does the incrementAndGet() method of AtomicInteger return?
It atomically increments the current value by 1 and returns the updated value, similar to the ++value operation in primitives but thread-safe
Name two AtomicInteger methods that are equivalent to primitive increment operations.
incrementAndGet() (pre-increment) and getAndIncrement() (post-increment) mirror ++value and value++, respectively, but perform them atomically
In which situations should AtomicInteger be avoided?
Avoid AtomicInteger when you need to atomically update multiple variables together. For such cases, use synchronized blocks or locks instead
What is the purpose of the addAndGet(int delta) method in AtomicInteger?
addAndGet(int delta) atomically adds the given value (delta) to the current value and returns the updated result. It is equivalent to value += delta, but thread-safe
What is the difference between addAndGet(int delta) and getAndAdd(int delta) in AtomicInteger?
addAndGet(int delta) returns the value after addition, whereas getAndAdd(int delta) returns the value before the addition, similar to pre-increment vs. post-increment behavior
What causes a ConcurrentModificationException in Java?
It occurs when a collection is modified while being iterated, using a method like remove() outside the iterator.
Which collections are most likely to throw ConcurrentModificationException?
Fail-fast collections like ArrayList, HashMap, and HashSet when modified during iteration.
How can you safely remove elements during iteration?
Use the iterator’s remove() method, or use a concurrent collection like CopyOnWriteArrayList or ConcurrentHashMap.
Why is using Collection.remove() inside a loop risky in a concurrent context?
It can cause a ConcurrentModificationException if the collection is being modified while iterating over it.
What is the correct way to remove elements from a collection during iteration?
Use the iterator’s remove() method, like this:
Iterator<’String’> it = list.iterator();
while (it.hasNext()) {
if (condition) it.remove();
}
How can you handle safe concurrent removals in Java?
Use concurrent collections like CopyOnWriteArrayList or ConcurrentLinkedQueue, which are designed for thread-safe access.