Reputation: 6364
What really is Atomic? since I have to choose some language to frame my question more clearly I would choose Java. I know that atomic means do everything or just rollback/do nothing. so say I have the following
public class Myclass {
private boolean progress;
public void setProgress(boolean progress) {
this.progress = progress;
}
public boolean getProgress() {
return progress;
}
}
Now which of the following are thread-safe or atomic or both ? please treat each new line as a separate piece of code
-------------------
getProgress(); //
------------------
----------------------
setProgress(true); //
----------------------
-------------------
getProgress()
setProgress();
-------------------
--------------------
setProgress();
getProgress();
--------------------
Which of these cases would make sense to use AtomicReference in java?
Upvotes: 0
Views: 81
Reputation: 1552
From this:
What operations in Java are considered atomic?
I'd say none of them is atomic, since function call is not an atomic operation. But once you are in a function, assigning a boolean is atomic (just that line), returning it isn't.
For thread safety take a look at this:
Basically the old value of "progress" maybe cached inside cpu, so even if assigning it a new value was atomic (just that = line again) without a synched assignment (AtomicBoolean or synchronized getter/setter) it is thread-safe however there can be memory consistency errors so you may want to declare that variable volatile so other threads can see the most updated value.
Upvotes: 2