Re: AtomicXXXX.weakCompareAndSet source ... ?
Thomas Hawtin <usenet@tackline.plus.com> wrote or quoted in
Message-ID: <45644853$0$8731$ed2619ec@ptn-nntp-reader02.plus.net>:
It seems to me that 'weakCompareAndSet' is identical
with 'compareAndSet'.
In the current Sun implementation.
What difference between them ?
The specification.
"AtomicXXX.addAndGet" is so, too ?
The specification says that "AtomicXXX" support thread-safe
programming on single variables.
But, it seems to me that the implementation is not safe in a
multithreaded environment in some senses. "addAndGet" or
"incrementAndGet" etc are not atomic methods, accordingly
the calling order of them is not preserved. Moreover it is
possible that they can never finish their tasks at the worst.
This code is a part of "AtomicLong" source.
<code>
public final long addAndGet(long delta) {
for (;;) {
long current = get(); // P1
long next = current + delta;
if (compareAndSet(current, next)) // P2
return next;
}
}
public final long get() {
return value; // volatile
}
<code>
For Thread 0, 1 , .. N.
Between P1 and P2 of Thread_I, it is practicable that
Thread_J calls 'get()' and then finishs his task earlier
than Thread_I. Moreover Thread_J can call 'get()'
repeatedly with high priority.
Is my thinking wrong ?