MyRunnable2.java
public class MyRunnable2 implements Runnable{
public boolean flag = true;
int i = 0;
public MyRunnable2() {
flag = true;
}
@Override
public void run() {
while(flag) {
System.out.println(Thread.currentThread().getName() +"=="+(i++) + flag);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Test4.java
public class Test4 {
public static void main(String[] args) {
MyRunnable2 mr2 = new MyRunnable2();
Thread t = new Thread(mr2);
t.start();
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName()+"-"+i);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (i ==20) {
mr2.flag = false;
}
}
}
}
在Test4.java中,主线程for循环到i为20的时候 mr2.flag设置为false,这样就可以控制t进程不再进行
但是它运行了,而且flag还是false,虽然后面t进程被中断了。
我想问的是:为什么flag为flase它还可以运行一次