我有一个函数,一旦布尔变量为真,就需要调用它。我尝试在线程中使用 while 循环,但它不起作用。这是我尝试过的:
public class MyRunnable implements Runnable {
public void run() {
while (true) {
if (conditions == true) {
System.out.println("second");
break;
}
}
}
public static void main(String args[]) {
boolean condition = false;
(new Thread(new MyRunnable())).start();
System.out.println("first\n");
// set conndition to true
condition = true;
}
}
结果应该是:
first
second