java - What really is Atomic? -
what atomic? since have choose language frame question more choose java. know atomic means or rollback/do nothing. have following
public class myclass { private boolean progress; public void setprogress(boolean progress) { this.progress = progress; } public boolean getprogress() { return progress; } }
now of following thread-safe or atomic or both ? please treat each new line separate piece of code
------------------- getprogress(); // ------------------ ---------------------- setprogress(true); // ---------------------- ------------------- getprogress() setprogress(); ------------------- -------------------- setprogress(); getprogress(); --------------------
which of these cases make sense use atomicreference in java?
from this:
what operations in java considered atomic?
i'd none of them atomic, since function call not atomic operation. once in function, assigning boolean atomic (just line), returning isn't.
for thread safety take @ this:
basically old value of "progress" maybe cached inside cpu, if assigning new value atomic (just = line again) without synched assignment (atomicboolean or synchronized getter/setter) thread-safe there can memory consistency errors may want declare variable volatile other threads can see updated value.
Comments
Post a Comment