R
raphfrk
Are booleans inherently thread safe?
I am thinking of something like
public class AsyncBoolean {
boolean value = false;
public AsyncBoolean(boolean initValue) {
value = initValue;
}
public boolean getValue() {
return value;
}
public void setValue(boolean value) {
value = newValue;
}
}
Assuming I define the variable using
final AsyncBoolean varName = new AsyncBoolean(true);
does this in effect auto-sync, since you can't partially read a byte
and even if you could it is a boolean anyway?
Since it is declared as final, the reference can't change, so I could
then call varName.setValue(someValue) and varName.getValue() from
threads without the need for syncing.
I am thinking of something like
public class AsyncBoolean {
boolean value = false;
public AsyncBoolean(boolean initValue) {
value = initValue;
}
public boolean getValue() {
return value;
}
public void setValue(boolean value) {
value = newValue;
}
}
Assuming I define the variable using
final AsyncBoolean varName = new AsyncBoolean(true);
does this in effect auto-sync, since you can't partially read a byte
and even if you could it is a boolean anyway?
Since it is declared as final, the reference can't change, so I could
then call varName.setValue(someValue) and varName.getValue() from
threads without the need for syncing.