Are you taking CLEP classes? Try clep.ai
| 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 2 students

Java Concurrency Livelock Issue

Mark
< 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 deadlock arising from circular waiting on locks.

B

Thread starvation due to unfair scheduling.

C

A race condition causing unpredictable outputs.

D

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

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Total Time
Score
#1busemagngr22 0m 55s 145
#2test111101 0m 03s -13
#3mavericklin12502 0m 00s -20
Items per page:
10
1 – 3 of 3

AI Tutor

How can I help?

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