Is iinc atomic in Java?
No its not
- Retrieve the current value of c.
- Increment the retrieved value by 1.
- Store the incremented value back in c.
Java Documentation for Atomicity and Thread Interference
You need to either use synchronized
keyword or use AtomicXXX
methods for Thread safety.
UPDATE:
public synchronized void increment() {
c++;
}
or
AtomicInteger integer = new AtomicInteger(1);
//somewhere else in code
integer.incrementAndGet();
Also read: Is iinc atomic in Java?
Not it's not and it can cause real problems. This test is supposed to print 200000000 but it doesnt due to thread interference
static int n;
public static void main(String[] args) throws InterruptedException {
Runnable r = new Runnable() {
public void run() {
for(int i = 0; i < 100000000; i++) {
n++;
}
}
};
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(n);
}
Note that volatile
does not solve the problem.
The answer to your question depends on whether you mean the IINC
instruction or, what other answers are referring to, the ++
operator.
Using ++
on a static or instance field is nothing more than get, increment, and set, thus it is not atomic (the other answers explain this in more detail).
But
Since you asked if the IINC
instruction is atomic, this is not the real answer. In fact, none of the answers to this question address the instruction, all of them seem to be based around the operator being used on instance or static fields.
The IINC
instruction only operates on local variables. As the name suggests, they are only local, and only accessible from a very limited scope. Thus, it is not possible to access a local variable from another Thread. This means that it doesn't matter whether or not the instruction is atomic.