I am wondering if this question can be solved in Java (I\'m new to the language). This is the code:
class Condition {
// you can change in the main
p
Not sure if this is an option but changing x from local variable to a field would allow other thread to change its value between the reading left and right side in if statement.
Here is short demo:
class Test {
static int x = 0;
public static void main(String[] args) throws Exception {
Thread t = new Thread(new Change());
t.setDaemon(true);
t.start();
while (true) {
if (x == x) {
System.out.println("Ok");
} else {
System.out.println("Not ok");
break;
}
}
}
}
class Change implements Runnable {
public void run() {
while (true)
Test.x++;
}
}
Output:
⋮
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Not ok