-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
17
-
generic
-
generic
ADDITIONAL SYSTEM INFORMATION :
Windows 11, x64
A DESCRIPTION OF THE PROBLEM :
When you write your custom spinlock, it doesn’t work with ++i, but it works with i+1
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
write code
ACTUAL -
0 and deadlock
---------- BEGIN SOURCE ----------
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
private static AtomicInteger atomicInteger = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 100; ++i) {
var t1 = new Thread(() -> {
for(;;) {
int saved = atomicInteger.get();
int newValue = ++saved;
if(atomicInteger.compareAndSet(saved, newValue)) {
return;
}
}
});
var t2 = new Thread(() -> {
for(;;) {
int saved = atomicInteger.get();
int newValue = ++saved;
if(atomicInteger.compareAndSet(saved, newValue)) {
return;
}
}
});
t1.start();
t2.start();
}
Thread.sleep(1000);
System.out.println(atomicInteger.get());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
private static AtomicInteger atomicInteger = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 100; ++i) {
var t1 = new Thread(() -> {
for(;;) {
int saved = atomicInteger.get();
int newValue = saved + 1;
if(atomicInteger.compareAndSet(saved, newValue)) {
return;
}
}
});
var t2 = new Thread(() -> {
for(;;) {
int saved = atomicInteger.get();
int newValue = saved + 1;
if(atomicInteger.compareAndSet(saved, newValue)) {
return;
}
}
});
t1.start();
t2.start();
}
Thread.sleep(1000);
System.out.println(atomicInteger.get());
}
}
FREQUENCY : always
Windows 11, x64
A DESCRIPTION OF THE PROBLEM :
When you write your custom spinlock, it doesn’t work with ++i, but it works with i+1
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
write code
ACTUAL -
0 and deadlock
---------- BEGIN SOURCE ----------
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
private static AtomicInteger atomicInteger = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 100; ++i) {
var t1 = new Thread(() -> {
for(;;) {
int saved = atomicInteger.get();
int newValue = ++saved;
if(atomicInteger.compareAndSet(saved, newValue)) {
return;
}
}
});
var t2 = new Thread(() -> {
for(;;) {
int saved = atomicInteger.get();
int newValue = ++saved;
if(atomicInteger.compareAndSet(saved, newValue)) {
return;
}
}
});
t1.start();
t2.start();
}
Thread.sleep(1000);
System.out.println(atomicInteger.get());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
private static AtomicInteger atomicInteger = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 100; ++i) {
var t1 = new Thread(() -> {
for(;;) {
int saved = atomicInteger.get();
int newValue = saved + 1;
if(atomicInteger.compareAndSet(saved, newValue)) {
return;
}
}
});
var t2 = new Thread(() -> {
for(;;) {
int saved = atomicInteger.get();
int newValue = saved + 1;
if(atomicInteger.compareAndSet(saved, newValue)) {
return;
}
}
});
t1.start();
t2.start();
}
Thread.sleep(1000);
System.out.println(atomicInteger.get());
}
}
FREQUENCY : always