| preferred AP College board partner for AP classes
AP Computer Science A/Unit 1: Primitive Types
Start Practice TestPractice Test
About Exam
hard Solved by 1 students
Java Concurrency Livelock Issue
< Prev
Next >

The following code defines two tasks intended to run concurrently within the LivelockExample class. When the main method is executed, which concurrency issue is most likely to occur?

public class LivelockExample {
    private volatile boolean flag1 = false;
    private volatile boolean flag2 = false;
    
    public void task1() {
        while (!flag2) {
            flag1 = true;
            Thread.yield();
        }
        System.out.println("Task1 completed");
    }
    
    public void task2() {
        while (!flag1) {
            flag2 = true;
            Thread.yield();
        }
        System.out.println("Task2 completed");
    }
    
    public static void main(String[] args) {
        LivelockExample le = new LivelockExample();
        Thread t1 = new Thread(le::task1);
        Thread t2 = new Thread(le::task2);
        t1.start();
        t2.start();
    }
}
A

A livelock where both tasks continuously yield without making progress due to mutual dependency.

B

A race condition causing unpredictable outputs.

C

A deadlock arising from circular waiting on locks.

D

Thread starvation due to unfair scheduling.

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: [email protected]|Privacy Policy