Wk5L1 - Locks Flashcards

1
Q

What is a ReentrantLock?

A

A ReentrantLock allows the same thread to acquire the lock multiple times. It maintains a count of how many times it has been locked. All locks must be released before another thread can execute.

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

When is a Reader-Writer Lock useful?

A

A Reader-Writer Lock is useful when multiple threads frequently read shared data, but only occasionally write to it. It allows multiple threads to read simultaneously but only one thread to write.

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

What are the two types of locks in a Reader-Writer Lock?

A

Shared Read Lock: Allows multiple threads to read the variable simultaneously (ReentrantReadWriteLock.ReadLock).

Exclusive Write Lock: Allows only one thread to modify the variable at a time (ReentrantReadWriteLock.WriteLock).

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

What is the Fairness Policy in ReentrantLocks?

A

The fairness policy ensures that the thread waiting the longest gets access to the lock. You can enable this by passing true to the lock’s constructor.

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

What are the downsides of using the Fairness Policy?

A

While the fairness policy ensures the longest waiting thread gets the lock, it can result in slower overall execution time, as thread scheduling is controlled by the operating system.

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

How does a ReentrantReadWriteLock improve performance for read-heavy applications?

A

By allowing multiple threads to read simultaneously without blocking, a ReentrantReadWriteLock reduces waiting time for threads that only need to read data, improving overall throughput.

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