Why My Code is working perfectly without the using of synchronization ..?

i am facing something confusion with my code behavior in my operation system .. You all now that there is a lot of ways to make a synchronization for multi-threading in order to get the right value no matter what !

so how come I am always get the right value Without using the a synchronized way ??? !!!

for example look into the below code

In regular behavior the below Program should terminated after a few second .. as there are ten threads accessing the very same variable and increment it by one .. and that should lead to have a value of count other than 100000 in some cases .. and that will stop the loop .. I am running this code over 20 Minutes .. and it worked perfectly ..

can anyone tell me what is going on :D ??

my operation system is windows 7 and I am using eclipse Kepler .. the JVM is 8 .. and my CPU is not Dual Core .. it's a regular solo .. with 2.4 GHZ ...

public class Worker {
    int count;

    public static void main(String[] args) {
        new Worker().run();
    }

    public void run() {
        do {
            count = 0;
            Thread thread1 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        count++;
                    }
                }
            });

            Thread thread2 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        count++;
                    }
                }
            });

            Thread thread3 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        count++;
                    }
                }
            });

            Thread thread4 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        count++;
                    }
                }
            });

            Thread thread5 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        count++;
                    }
                }
            });

            Thread thread6 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        count++;
                    }
                }
            });

            Thread thread7 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        count++;
                    }
                }
            });

            Thread thread8 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        count++;
                    }
                }
            });

            Thread thread9 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        count++;
                    }
                }
            });

            Thread thread10 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        count++;
                    }
                }
            });

            thread1.start();
            thread2.start();
            thread3.start();
            thread4.start();
            thread5.start();
            thread6.start();
            thread7.start();
            thread8.start();
            thread9.start();
            thread10.start();

            try {
                thread1.join();
                thread2.join();
                thread3.join();
                thread4.join();
                thread5.join();
                thread6.join();
                thread7.join();
                thread8.join();
                thread9.join();
                thread10.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("Count is: " + count);
        } while (count == 100000);
    }
 }
Jon Skeet
people
quotationmark

I suspect your JVM happens to have generated code where count++ is JIT-compiled into an atomic operation... even though it's not guaranteed to... and your computer's memory model isn't as lax as it could be.

It's still unsafe - it's just that you happen to be getting away with it on your architecture.

people

See more on this question at Stackoverflow