I get the difference between synchronized(just on thread at a time can read/write a variable) and volatile(variable is always changed directly in slow memory, not loaded in Cashe), but I can not really imagine an example for the difference in use. Anyone got a good example?
You can declare a variable as volatile, but you cannot use synchronized for variables - only for:
- methods (public synchronized void ...)
- objects( synchronized (MyObject) {...})
- or classes (synchronized (MyClass.class){...}
GO TO FULL VERSION