Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 6: Array
Start Practice TestPractice Test
About Exam
hard Solved by 1 students

Java Concurrency and Deadlock

Mark
< Prev
Next >

Consider the MultiLock class shown below. Which of the following statements best describes a potential concurrency problem in this class?

public class MultiLock {
    private int[] arrayA = new int[2];
    private int[] arrayB = new int[2];
    
    public void methodOne() {
        synchronized(arrayA) {
            try { Thread.sleep(50); } catch (InterruptedException e) {}
            synchronized(arrayB) {
                arrayA[0] = 10;
            }
        }
    }
    
    public void methodTwo() {
        synchronized(arrayB) {
            try { Thread.sleep(50); } catch (InterruptedException e) {}
            synchronized(arrayA) {
                arrayB[0] = 20;
            }
        }
    }
    
    public static void main(String[] args) {
        MultiLock ml = new MultiLock();
        Thread t1 = new Thread(() -> ml.methodOne());
        Thread t2 = new Thread(() -> ml.methodTwo());
        t1.start();
        t2.start();
    }
}
A

Deadlock is impossible because the locked objects are mutable arrays, which cannot cause deadlock conditions.

B

Arrays are inherently thread-safe, so synchronizing on them is unnecessary and does not introduce any problems.

C

The sleep calls ensure that thread execution is serialized, thereby preventing any concurrency issues.

D

The lock ordering in methodOne and methodTwo is reversed, which can lead to a deadlock if both methods are executed concurrently.

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.

AI Tutor

How can I help?

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