Java Thread Synchronization Issues
Consider the FlagCheck class shown below. In a multi-threaded environment, what is the primary issue with this implementation?
public class FlagCheck {
private boolean flag = false;
public void setTrue() { flag = true; }
public void waitForFlag() {
while(!flag) {
// busy-wait
}
System.out.println("Flag is true");
}
}
A
Race condition and memory visibility issues because ‘flag’ is neither volatile nor accessed within a synchronized block.
B
Deadlock since the busy-wait loop prevents lock release.
C
The program will always compile with no runtime issues.
D
An infinite loop due to improper use of the volatile keyword.
Question Leaderboard
Not enough data yet to show leaderboard.
