|
下記のプログラムで実行するたびに "合計10回"で終われば、 "合計11回"で終わることもあり、一体なぜこのようなこ とが起きるのでしょうか? 教えていただきたいです。
----------------------------------------------- public class Main { public static int count = 0; public static void main(String[] args) { ExThread1 thread1 = new ExThread1(); ExThread2 thread2 = new ExThread2(); thread1.start(); thread2.start(); } }
class ExThread1 extends Thread { public void run() { while(Main.count <= 20){ Main.count++; try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
class ExThread2 extends Thread { int a = 1; public void run() { while (Main.count <= 10) { if (a == Main.count) { System.out.println("合計何回わりこまれた" + " " + Main.count); a++; } } } }
|