109

I'm trying to deepen my understanding of Java's threading model.

To do this, I wrote a small test where I count how many times two threads increment a shared variable.

Here's what I did:

I declared a shared static variable in my main class:

private static int globalTotalCount = 0; 

I started two threads:

  • Thread 1: increments thread1Count

  • Thread 2: increments thread2Count

    Both threads also increment the shared variable globalTotalCount.

At the end, I expect that:

thread1Count + thread2Count == globalTotalCount 

However, in practice, the sum of the two thread counters does NOT match the final value of globalTotalCount. (most of the time)

Here is my code:

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** * Demonstrates a race condition between two threads incrementing shared counters. * The sum of thread-specific counts and the global total will often differ due to missing synchronization. */ public class RaceConditionDemo { // Shared counters (not thread-safe) private static int globalTotalCount = 0; private static int thread1Count = 0; private static int thread2Count = 0; // Flag to signal threads to stop; volatile ensures visibility between threads private volatile boolean shouldRun = true; public RaceConditionDemo() { // Create and use a thread pool for our worker threads try (ExecutorService executor = Executors.newCachedThreadPool()) { executor.execute(thread1Task); executor.execute(thread2Task); // Let threads run for 1 seconds try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } // Stop threads shouldRun = false; // Shutdown thread pool and wait for tasks to finish executor.shutdown(); try { if (!executor.awaitTermination(2, TimeUnit.SECONDS)) { executor.shutdownNow(); } } catch (InterruptedException e) { executor.shutdownNow(); Thread.currentThread().interrupt(); } } // Print results System.out.println("Result:"); System.out.println("\tThread 1 Count:\t" + thread1Count); System.out.println("\tThread 2 Count:\t" + thread2Count); System.out.println("--------------------------------"); System.out.println("\tSum of thread counts:\t" + (thread1Count + thread2Count)); System.out.println("\tGlobal total count:\t\t" + globalTotalCount); System.out.println("--------------------------------"); int difference = thread1Count + thread2Count - globalTotalCount; if (difference != 0) { System.out.println("Difference:\t" + difference + " => Race condition detected!"); } else { System.out.println("Difference:\t" + difference + " => No race condition detected! (this time)"); } } // Worker for thread 1: increments its own and the global counter, prints its progress private final Runnable thread1Task = () -> { while (shouldRun) { globalTotalCount++; thread1Count++; System.out.println("[Thread 1]\tCall:\t" + thread1Count + "\n"); try { Thread.sleep(1); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } }; // Worker for thread 2: increments its own and the global counter, prints its progress private final Runnable thread2Task = () -> { while (shouldRun) { globalTotalCount++; thread2Count++; System.out.println("[Thread 2]\tCall:\t" + thread2Count + "\n"); try { Thread.sleep(1); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } }; public static void main(String[] args) { new RaceConditionDemo(); } } 
8
  • 16
    Why don't you try with AtomicInteger? Commented Aug 6, 2014 at 18:56
  • 7
    See also: another SO question, ++ not considered atomic, Concurrency in Java. Commented Aug 6, 2014 at 19:11
  • 4
    the JVM has an iinc operation for incrementing integers, but that only works for local variables, where concurrency is not a concern. For fields, the compiler generates read-modify-write commands separately. Commented Aug 6, 2014 at 21:24
  • 16
    Why would you even expect it to be atomic? Commented Aug 6, 2014 at 21:55
  • 2
    @Silly Freak: even if there was an iinc instruction for fields, having a single instruction does not guarantee atomicity, e.g. non-volatile long and double field access in not guaranteed to be atomic regardless of the fact that it is performed by a single bytecode instruction. Commented Aug 7, 2014 at 10:41

11 Answers 11

138

i++ is probably not atomic in Java because atomicity is a special requirement which is not present in the majority of the uses of i++. That requirement has a significant overhead: there is a large cost in making an increment operation atomic; it involves synchronization at both the software and hardware levels that need not be present in an ordinary increment.

You could make the argument that i++ should have been designed and documented as specifically performing an atomic increment, so that a non-atomic increment is performed using i = i + 1. However, this would break the "cultural compatibility" between Java, and C and C++. As well, it would take away a convenient notation which programmers familiar with C-like languages take for granted, giving it a special meaning that applies only in limited circumstances.

Basic C or C++ code like for (i = 0; i < LIMIT; i++) would translate into Java as for (i = 0; i < LIMIT; i = i + 1); because it would be inappropriate to use the atomic i++. What's worse, programmers coming from C or other C-like languages to Java would use i++ anyway, resulting in unnecessary use of atomic instructions.

Even at the machine instruction set level, an increment type operation is usually not atomic for performance reasons. In x86, a special instruction "lock prefix" must be used to make the inc instruction atomic: for the same reasons as above. If inc were always atomic, it would never be used when a non-atomic inc is required; programmers and compilers would generate code that loads, adds 1 and stores, because it would be way faster.

In some instruction set architectures, there is no atomic inc or perhaps no inc at all; to do an atomic inc on MIPS, you have to write a software loop which uses the ll and sc: load-linked, and store-conditional. Load-linked reads the word, and store-conditional stores the new value if the word has not changed, or else it fails (which is detected and causes a re-try).

Sign up to request clarification or add additional context in comments.

6 Comments

as java has no pointers, incrementing local variables is inherently thread save, so with loops the problem mostly wouldn't be so bad. your point about least surprise stands, of course. also, as it is, i = i + 1 would be a translation for ++i, not i++
The first word of the question is "why". As of now, this is the only answer to address the issue of "why". The other answers really just re-state the question. So +1.
It might be worth noting that an atomicity guaranty would not solve the visibility issue for updates of non-volatile fields. So unless you will treat every field as implicitly volatile once one thread has used the ++ operator on it, such an atomicity guaranty would not solve concurrent update issues. So why potentially wasting performance for something if it doesn’t solve the problem.
@DavidWallace don't you mean ++? ;)
"Even at the machine instruction set level, an increment type operation is usually not atomic for performance reasons." Is it because it involves "multiple steps" at machine instruction level?
|
42

i++ involves two operations :

  1. read the current value of i
  2. increment the value and assign it to i

When two threads perform i++ on the same variable at the same time, they may both get the same current value of i, and then increment and set it to i+1, so you'll get a single incrementation instead of two.

Example :

int i = 5; Thread 1 : i++; // reads value 5 Thread 2 : i++; // reads value 5 Thread 1 : // increments i to 6 Thread 2 : // increments i to 6 // i == 6 instead of 7 

8 Comments

+1, but "1. A, 2. B and C" sounds like three operations, not two. :)
Note that even if the operation were implemented with a single machine instruction that incremented a storage location in place, there is no guarantee it would be thread-safe. The machine still needs to fetch the value, increment it, and store it back, plus there may be multiple cache copies of that storage location.
@Aquarelle - If two processors execute the same operation against the same storage location simultaneously, and there is no "reserve" broadcast on the location, then they will almost certainly interfere and produce bogus results. Yes, it is possible for this operation to be "safe", but it takes special effort, even at the hardware level.
But I think the question was "Why" and not "What happens".
Thank you; however, I think, that 1) read current; 2)increment; 3) assign it back is still three operations :)
|
16

Java specification

The important thing is the JLS (Java Language Specification) rather than how various implementations of the JVM may or may not have implemented a certain feature of the language.

The JLS defines the ++ postfix operator in clause 15.14.2. To quote:

… the value 1 is added to the value of the variable and the sum is stored back into the variable. …

Nowhere does it mention or hint at multithreading or atomicity.

For multithreading or atomicity, the JLS provides volatile and synchronized. Additionally, there are the Atomic classes.

Comments

6

Why is i++ not atomic in Java?

Let's break the increment operation into multiple statements:

Thread 1 & 2 :

  1. Fetch value of total from memory
  2. Add 1 to the value
  3. Write back to the memory

If there is no synchronization then let's say Thread one has read the value 3 and incremented it to 4, but has not written it back. At this point, the context switch happens. Thread two reads the value 3, increments it and the context switch happens. Though both threads have incremented the total value, it will still be 4 - race condition.

5 Comments

I don't get how this should be an answer to the question. A language can define any feature as atomic, be it increments or unicorns. You just exemplify a consequence of not being atomic.
Yes a language can define any feature as atomic but as far as java is considered increment operator(which is the question posted by OP) is not atomic and my answer states the reasons.
(sorry for my harsh tone in first comment) But then, the reason seems to be "because if it would be atomic, then there would be no race conditions". I.e., it sounds as if a race condition is desirable.
@phresnel the overhead introduced to keep an increment atomic is huge and rarely desired, keeping the operation cheap and as a result non atomic is desirable most of the time.
@josefx: Note that I am not questioning the facts, but the reasoning in this answer. It basically says "i++ is not atomic in Java because of the race conditions it has", which is like saying "a car has no airbag because of the crashes that can happen" or "you get no knife with your currywurst-order because the wurst may need to be cut". Thus, I don't think this is an answer. The question was not "What does i++ do?" or "What is the consequence of i++ not being synced?".
5

i++ is a statement which simply involves 3 operations:

  1. Read current value
  2. Write new value
  3. Store new value

These three operations are not meant to be executed in a single step or in other words i++ is not a compound operation. As a result all sorts of things can go wrong when more than one threads are involved in a single but non-compound operation.

Consider the following scenario:

Time 1:

Thread A fetches i Thread B fetches i 

Time 2:

Thread A overwrites i with a new value say -foo- Thread B overwrites i with a new value say -bar- Thread B stores -bar- in i // At this time thread B seems to be more 'active'. Not only does it overwrite // its local copy of i but also makes it in time to store -bar- back to // 'main' memory (i) 

Time 3:

Thread A attempts to store -foo- in memory effectively overwriting the -bar- value (in i) which was just stored by thread B in Time 2. Thread B has nothing to do here. Its work was done by Time 2. However it was all for nothing as -bar- was eventually overwritten by another thread. 

And there you have it. A race condition.


That's why i++ is not atomic. If it was, none of this would have happened and each fetch-update-store would happen atomically. That's exactly what AtomicInteger is for and in your case it would probably fit right in.

P.S.

An excellent book covering all of those issues and then some is this: Java Concurrency in Practice

4 Comments

Hmm. A language can define any feature as atomic, be it increments or unicorns. You just exemplify a consequence of not being atomic.
@phresnel Exactly. But I also point out that it's not a single operation which by extension implies that the computational cost for turning multiple such operations into atomic ones is much more expensive which in turn -partially- justifies why i++ is not atomic.
While I get your point, your answer is a bit confusing to the learning. I see an example, and a conclusion that says "because of the situation in the example"; imho this is an incomplete reasoning :(
@phresnel Maybe not the most pedagogical answer but it's the best I can currently offer. Hopefully it will help people and not confuse them. Thanks for critisism however. I 'll try to be more precise in my future posts.
2

In the JVM, an increment involves a read and a write, so it's not atomic.

Comments

1

If the operation i++ would be atomic you wouldn't have the chance to read the value from it. This is exactly what you want to do using i++ (instead of using ++i).

For example look at the following code:

public static void main(final String[] args) { int i = 0; System.out.println(i++); } 

In this case we expect the output to be: 0 (because we post increment, e.g. first read, then update)

This is one of the reasons the operation can't be atomic, because you need to read the value (and do something with it) and then update the value.

The other important reason is that doing something atomically usually takes more time because of locking. It would be silly to have all the operations on primitives take a little bit longer for the rare cases when people want to have atomic operations. That is why they've added AtomicInteger and other atomic classes to the language.

4 Comments

This is misleading. You have to separate execution and getting the result, otherwise you couldn't get values from any atomic operation.
No it isn't, that is why Java's AtomicInteger has a get(), getAndIncrement(), getAndDecrement(), incrementAndGet(), decrementAndGet() etc.
And the Java-language could have defined i++ to be expanded to i.getAndIncrement(). Such expanding isn't new. E.g., lambdas in C++ are expanded to anonymous class definitions in C++.
Given an atomic i++ one can trivially create an atomic ++i or vice-versa. One is equivalent to the other plus one.
1

There are two steps:

  1. fetch i from memory
  2. set i+1 to i

so it's not atomic operation. When thread1 executes i++, and thread2 executes i++, the final value of i may be i+1.

Comments

1

In Java, the i++ operation is not atomic because it is actually made up of several separate steps that can be interrupted by other threads. Specifically, i++ consists of three actions:

  1. reading the current value of i
  2. incrementing that value by 1
  3. writing the updated value back to i.

Each of these actions happens independently, so other threads can run in between them, which can cause problems.

This can lead to a race condition if multiple threads try to increment i at the same time.

For example, two threads might both read the same original value of i, increment it, and then both write back their results—so one increment is "lost" and the final value is lower than expected.

To avoid this problem and ensure atomic operations, Java provides the AtomicInteger class. (i was not aware of)

Comments

-1

In JVM or any VM, the i++ is equivalent to the following:

int temp = i; // 1. read i = temp + 1; // 2. increment the value then 3. write it back 

that is why i++ is non-atomic.

1 Comment

That is how it is non-atomic, not why, as so many of the other answers and comments have covered.
-2

Concurrency (the Thread class and such) is an added feature in v1.0 of Java. i++ was added in the beta before that, and as such is it still more than likely in its (more or less) original implementation.

It is up to the programmer to synchronize variables. Check out Oracle's tutorial on this.

Edit: To clarify, i++ is a well defined procedure that predates Java, and as such the designers of Java decided to keep the original functionality of that procedure.

The ++ operator was defined in B (1969) which predates java and threading by just a tad.

7 Comments

-1 "public class Thread ... Since: JDK1.0" Source: docs.oracle.com/javase/7/docs/api/index.html?java/lang/…
The version doesn't matter so much as the fact that it was still implemented before the Thread class and was not changed because of it, but I've edited my answer to please you.
What matters is that your claim "it was still implemented before the Thread class" is not backed by sources. i++ not being atomic is a design decision, not an oversight in a growing system.
Just because a language feature is borrowed from or inspired by a corresponding feature in other languages doesn't mean it necessarily keeps the exact same underlying characteristics. Consider, for instance, the variety of lambda functions in different languages (e.g. the weakness of Python's lambda compared to true Lisp lambdas symbo1ics.com/blog/?p=1292). Even in C++, certain features taken from C aren't quite identical to their C counterparts, despite the fact that C++ is often thought to be backwards-compatible with C (cprogramming.com/tutorial/c-vs-c++.html).
@TheBat: But many borrowed keywords and operators are not implemented like in the language/s they were borrowed from. E.g., class and operator. in C++ are totally orthogonal to their Java "equivalents".
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.