Why is this java class not Thread safe.
class TestClass { private int x; int get() { return x; } void set(int x) { this.x = x;
When you have two method modifying/accessing a non-volatile variable it is never thread safe. If you want to have just one method you can try :
synchronized int getAndSet(int x, boolean set) { if (set) this.x = x; return this.x; // param x is for set }