Why the pool does not set to 5 in the f() method that is called by another thread?
public class Main {        
        private static int pool = 5;        
        public static void main(String... arg) {        
            Th t = new Th();
            Thread thread = new Thread(t);
            thread.start();
            while (true) {        
               if (pool >= 0)
                    System.out.println(pool--);
            }       
        }
        static void f() {
            pool = 5;
            System.out.println("main.Main.f:pool=" + pool);
        }       
    }
    class Th implements Runnable {        
        @Override
        public void run() {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Main.f();        
            }
        }
    }
Output:
5
4
3
2
1
0
main.Main.f:pool=5
main.Main.f:pool=5
main.Main.f:pool=5
main.Main.f:pool=5
I can fix it declaring pool as volatile but I can't get why it works 8(
 
     
    