Declare Value
as volatile
I.e. static volatile boolean Value =false;
You have declared your set/get
methods synchronized
. This means that they are lock on this
(the object's intrinsic lock).
But in your code you instantiate a different ThreadResource
for each thread thereby not making them synchronized
since this
is different for each case.
Change your code as follows:
public class MyThreadA implements Runnable {
ThreadResource tR;
public MyThreadA(ThreadResource tr) {
this.tR = tr;
}
// your run method here NOT declaring a ThreadResource anymore!!!
}
and same for MyThreadB
Then in ThreadExecutionPoint
ThreadResource tr = new ThreadResource();
Thread th1 = new Thread(new MyThreadA(tr));
Thread th2 = new Thread(new MyThreadB(tr));