Java Concurrency Livelock Issue
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.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE