I am writing some java code and have a contrived example, and am not really sure if this is thread safe code.
Basically I have a while loop that checks a bool, and I want to set that bool false from another thread.
Observe the following
public void start() {
mRunning = true;
thread = new Thread()
{
public void run()
{
while (mRunning) {
}
}
}
thread.start();
}
Then I have a mutator on the class that can be called from another thread...
public void stop() {
mRunning = false;
}
Could this code result in undesirable behaviour? If so, should I just just do this?
public void stop() {
sychronized(this) {
mRunning = false;
}
}