public class Main {
public static void main(String[] args) throws InterruptedException {
Test test = new Test();
Thread thread1 = new Thread(() -> {
try {
System.out.println(test.change());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread thread2 = new Thread(() -> {
try {
System.out.println(test.change());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread1.start();
TimeUnit.MILLISECONDS.sleep(8);
thread2.start();
}
}
public class Test {
private volatile int a = 0;
public int change() throws InterruptedException {
while (a == 0){
TimeUnit.MILLISECONDS.sleep(10);
a++;
}
return a;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/251044.html
標籤:Java SE
上一篇:為什么一個maven專案,引入log4j,控制臺啥也不列印啊?
下一篇:Java亂數
