[1] It is believed that R. Kent Treiber was the first to publish it in his 1986 article "Systems Programming: Coping with Parallelism".
If the two are matching then you can swap old head to the new one, if not then it means another thread has added an item to the stack, in which case you must try again.
In some languages—particularly, those without garbage collection—the Treiber stack can be at risk for the ABA problem.
However, the Java version on this page is not subject to this problem, because of the stronger guarantees offered by the Java runtime (it is impossible for a newly created, unaliased object reference to be reference-equal to any other reachable object.)
Testing for failures such as ABA can be exceedingly difficult, because the problematic sequence of events is very rare.