Chapter 13: Concurrency Flashcards

1
Q

A process is a group of associated threads that execute in the same shared environment.
If false, why?

A

True.

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

A thread is the smallest unit of execution that can be scheduled by the operating system. If false, why?

A

True.

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

A single-threaded process contains exactly one thread, whereas a multi-threaded process supports more than one thread.
If false, why?

A

True.

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

A single process has shared memory between its threads.
If false, why?

A

True.

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

A task is a single unit of work performed by a thread.
If false, why?

A

True.

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

The property of executing multiple threads and processes at the same time is referred to as concurrency.
If false, why?

A

True.

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

Operating systems use a thread scheduler to determine which threads should be currently executing.
If false, why?

A

True

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

Local variables are shared among all threads within a process.
If false, why?

A

Local variables are stored on the stack and are thread-specific.
(False)

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

Static methods and variables are unique to each thread within a process.
If false, why?

A

Static methods and variables are shared among all threads in the same process.
(False)

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

A context switch occurs when a thread completes execution and terminates.
If false, why?

A

A context switch happens when a thread’s execution is paused to allow another thread to run.
(False)

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

A thread scheduler guarantees an equal amount of CPU time for every thread.
If false, why?

A

The scheduler may use different strategies, and priorities can affect execution time.
(False)

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

A thread cannot interrupt another thread, even if it has a higher priority.
If false, why?

A

A higher-priority thread can supersede a lower-priority thread in scheduling.
(False)

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

CPUs are directly connected to the OS thread scheduler.
If false, why?

A

The OS thread scheduler manages threads, but CPUs execute instructions independently.
(False)

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

A thread can execute multiple tasks at the same time.
If false, why?

A

A thread can only execute one task at a time but can switch between tasks.
(False)

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

What is the purpose of the AtomicInteger class in Java?

A

AtomicInteger is a class from java.util.concurrent.atomic that allows atomic (thread-safe) operations on int values without using synchronisation or locks​

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

Why is AtomicInteger preferred over volatile int for concurrent updates?

A

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

17
Q

What does the incrementAndGet() method of AtomicInteger return?

A

It atomically increments the current value by 1 and returns the updated value, similar to the ++value operation in primitives but thread-safe

18
Q

Name two AtomicInteger methods that are equivalent to primitive increment operations.

A

incrementAndGet() (pre-increment) and getAndIncrement() (post-increment) mirror ++value and value++, respectively, but perform them atomically

19
Q

In which situations should AtomicInteger be avoided?

A

Avoid AtomicInteger when you need to atomically update multiple variables together. For such cases, use synchronized blocks or locks instead​

20
Q

What is the purpose of the addAndGet(int delta) method in AtomicInteger?

A

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

21
Q

What is the difference between addAndGet(int delta) and getAndAdd(int delta) in AtomicInteger?

A

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​

22
Q

What causes a ConcurrentModificationException in Java?

A

It occurs when a collection is modified while being iterated, using a method like remove() outside the iterator.

23
Q

Which collections are most likely to throw ConcurrentModificationException?

A

Fail-fast collections like ArrayList, HashMap, and HashSet when modified during iteration.

24
Q

How can you safely remove elements during iteration?

A

Use the iterator’s remove() method, or use a concurrent collection like CopyOnWriteArrayList or ConcurrentHashMap.

25
Q

Why is using Collection.remove() inside a loop risky in a concurrent context?

A

It can cause a ConcurrentModificationException if the collection is being modified while iterating over it.

26
Q

What is the correct way to remove elements from a collection during iteration?

A

Use the iterator’s remove() method, like this:

Iterator<’String’> it = list.iterator();
while (it.hasNext()) {
if (condition) it.remove();
}

27
Q

How can you handle safe concurrent removals in Java?

A

Use concurrent collections like CopyOnWriteArrayList or ConcurrentLinkedQueue, which are designed for thread-safe access.