Chapter 4 - Concurrency Issues and Distributed Applications

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/40

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

41 Terms

1
New cards

What is the Readers and Writers problem?

Example:
One client reads while another deletes

2
New cards

How would concurrency be prevented in the bank example?

By ensuring one withdrawal completes entirely before another begins (i.e. making it atomic).

3
New cards

What is a deadlock?

A situation where two or more processes wait forever for each other to release resources, so nothing progresses.

4
New cards

What is livelock?

When threads keep changing state to avoid deadlock, but still make no progress (e.g., both step aside repeatedly).

5
New cards

What is lockout (or starvation)?

When one process is perpetually denied access to a resource while others continuously get it.

6
New cards

Can multiple readers access data simultaneously?

Yes, if no one is writing.

7
New cards

Can multiple writers write at the same time?

No, only one writer can modify data at a time.

8
New cards

What does “atomic” mean in the context of concurrency?

An operation that runs completely without interruption.

9
New cards

Is regular Java code atomic by default?

No – even a simple line like x = a + b; can be interrupted mid-execution.

10
New cards

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)

11
New cards

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.

12
New cards

What is a MUTEX?

Short for mutual exclusion – it ensures that only one thread can access a critical section of code at a time.

13
New cards

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.

14
New cards

What is the problem with unsynchronized operations like ++ and --?

They are not atomic and can be interrupted by other threads, leading to incorrect results.

15
New cards

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.

16
New cards

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.

17
New cards

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.

18
New cards

What does wait() do inside a synchronized block?

It pauses the thread and releases the lock, allowing others to proceed.

19
New cards

What does notifyAll() do?

It wakes up all waiting threads that have called wait() on the same object.

20
New cards

Why use notifyAll() instead of notify()?

Because notify() wakes just one thread (which might not be able to proceed), possibly causing deadlock.

21
New cards

Where should notifyAll() be called in readers-writers logic?

When a reader finishes and the counter becomes zero, allowing pending write threads to proceed.

22
New cards

What’s the correct reader thread structure?

  • counter.inc();

  • Perform read

  • counter.dec(); (if c == 0, call notifyAll())

23
New cards

What is the volatile keyword used for?

It prevents caching and ensures visibility and ordering of variable changes across threads.

24
New cards

Why don't we need volatile in the standard readers-writers pattern?

Because we already use synchronized blocks, which enforce memory consistency.

25
New cards

When can you use just wait() inside a method (without an object)?

In a synchronized method, where the method implicitly synchronizes on this.

26
New cards

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.

27
New cards

Is cloning inefficient?

No – it's a shallow copy, meaning only references are copied, not the objects themselves

28
New cards

Why is the cloning approach efficient?

Because the critical section (synchronized part) is very short – only as long as the cloning takes.

29
New cards

What is ConcurrentHashMap?

A thread-safe Java collection that supports concurrent read and write access without locking the whole map.

30
New cards

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.

31
New cards

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.

32
New cards

What is ReadWriteLock used for?

It allows multiple readers to access shared data simultaneously, but ensures exclusive access for writers.

33
New cards

How do you create a read-write lock in Java?

ReadWriteLock lock = new ReentrantReadWriteLock();

34
New cards

What is the syntax to acquire and release a read lock?

lock.readLock().lock();

// ... read operations ...

lock.readLock().unlock();

35
New cards

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

36
New cards

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

37
New cards

Does ReadWriteLock prevent reader lockout of writers?

No – readers can still lock out writers indefinitely if new readers continuously arrive (a theoretical lockout issue)

38
New cards

What advantage does ReadWriteLock offer over manual counters?

It hides the complexity of counting readers/writers and automatically handles synchronization.

39
New cards

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.

40
New cards

Does ReadWriteLock guarantee fairness between writers?

Yes – if multiple writers are waiting, the one waiting longest goes first.

41
New cards

What are the 4 ways to prevent concurrency issues?

  1. Locking

  2. Cloning

  3. Counters

  4. Synchronizing