Concurrency Problem in Double Check Locking
Consider the LazyInit class shown below, which uses a double-check locking pattern to lazily initialize a Resource object. Which of the following statements best describes a potential concurrency problem in this implementation?
public class LazyInit {
private Resource resource = null;
public Resource getResource() {
if(resource == null) {
synchronized(this) {
if(resource == null) {
resource = new Resource();
}
}
}
return resource;
}
}
class Resource {
// Resource implementation
}
A
Without declaring ‘resource’ as volatile, threads might see a partially constructed Resource due to possible instruction reordering.
B
The variable ‘resource’ should be final to ensure thread safety, and its absence leads to immediate failure.
C
The nested if statements inherently cause a deadlock between competing threads.
D
Double-check locking is redundant when using synchronized, so no concurrency issue exists.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE