While going through the Blocking/Non Blocking Algorithms section at the link
and the below code to explain the Atomic compareAndSet
operation
boolean updated = false; while(!updated){ long prevCount = this.count.get(); updated = this.count.compareAndSet(prevCount, prevCount + 1); }
It states that
Therefore no synchronization is necessary, and no thread suspension is necessary. This saves the thread suspension overhead.
Does it mean that if there are 2 threads that call compareAndSet()
at the same time in the above code, both of them will execute concurrently
or parallelly
which is in contrast to the synchronized
block where one thread gets blocked if both access simultaneously? If that's the case wouldn't the values get overriden in the above case? Which is same case when there is no synchronization?
没有评论:
发表评论