| preferred AP College board partner for AP classes
hard Solved by 1 students
Concurrency Problem in Double Check Locking
< Prev
Next >

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.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy