I know increment operation is not atomic in C++ without locking.
Will JVM add any lock on its implementation of iinc
instruction?
i++ is not atomic in java.
It is better to use
AtomicInteger atomic= new AtomicInteger(1);
There are methods defined like
atomic.getAndDecrement();
atomic.getAndIncrement();
atomic.decrementAndGet();
atomic.incrementAndGet();
any operation with above method would be atomic.
This class comes under java.util.concurrent.atomic
package.
Java 1.5
has added many features for thready safety and thread concurrency.
It is not atomic. Here is the generated byte code for an post-increment of a static int:
0 getstatic 18; // Load value of variable.
3 iconst_1;
4 iadd;
5 putstatic 18; // Store value.
{Although there already is an accepted answer, I wanted to clear something up, thus the late and technically unnecessary answer}
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.
No it is not atomic, the bytecode can get interleaved with other threads.
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.
No its not
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?