Javaキーワードvolatileの詳細
https://www.cnblogs.com/dolphin0520/p/3920373.html
public class HelloWorld {
boolean flag = true;
Executor executor = Executors.newFixedThreadPool(4);
public static void main(String[] args) {
new HelloWorld().run();
}
void run(){
executor.execute(new T1());
executor.execute(new T2());
}
class T1 implements Runnable{
public void run() {
while(flag){
int i = 0 ;
System.out.println(i++);
}
}
}
class T2 implements Runnable{
public void run(){
while(flag){
try {
Thread.sleep(1000);
flag = false ;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
eclipseを使用してテストしないでください.eclipseモードで使用するclientモードです.