Tud*_*dor 16 java multithreading synchronization nonblocking
正如标题所暗示的那样,我正在寻找一种比较和交换的实现,但是要比较大:
if(newValue > oldValue) {
oldValue = newValue;
}
Run Code Online (Sandbox Code Playgroud)
哪里oldValue是一些全局共享状态,并且newValue对每个线程都是私有的,而不执行以下操作:
synchronized(locker) {
if(newValue > oldValue) {
oldValue = newValue;
}
}
Run Code Online (Sandbox Code Playgroud)
因为我想要一个非阻塞的解决方案.通过研究其他非阻塞操作的源代码,我想出了这个(假设值是整数):
AtomicInteger oldValue; // shared global variable
...
public boolean GreaterThanCAS(int newValue) {
while(true) {
int local = oldValue;
if(local == oldValue) {
if(newValue > local) {
if(oldValue.compareAndSet(local, newValue) {
return true; // swap successful
} // else keep looping
} else {
return false; // swap failed
}
} // else keep looping
}
}
Run Code Online (Sandbox Code Playgroud)
当// else keep looping发生时,这意味着另一个线程oldValue在此期间已经改变了,所以我需要循环并再次尝试.
这个实现是否正确(线程安全)?
Vad*_*zim 14
从Java 8开始,这可以通过使用updateAndGet进行简化:
public boolean greaterThanCAS(int newValue) {
return oldValue.updateAndGet(x -> x < newValue ? newValue : x) == newValue;
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果旧值和新值相等,也会返回true.如果这不是所希望的行为,请尝试@ Adam的答案.
我认为你的实现没有问题,前提是没有线程会降低你的值AtomicInteger.如果他们这样做,您的代码将对竞争条件开放.
请注意,代码可以简化如下:
public boolean GreaterThanCAS(int newValue) {
while(true) {
int local = oldValue.get();
if(newValue <= local) {
return false; // swap failed
}
if(oldValue.compareAndSet(local, newValue)) {
return true; // swap successful
}
// keep trying
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5635 次 |
| 最近记录: |