All threads in java have their own call stack but they may share some shared resources. In case of shared resources visibility and access problems can occur.
Visibility problem: Visibility problem occurs when one thread (say first thread) reads a field value which is later updated by some other thread but changes are not guaranteed to be seen by first thread.
class TestThread extends Thread {
private boolean stop = false;
public void run() {
while(!stop) {
testTask();
}
}
public void setStop() {
this.stop = true;
}
}
Note: If stop is not volatile or setStop and run are not synchronized it is not guaranteed to work.
Access problem: It occurs when multiple threads access and change shared data at same time which may leads in incorrect data.